1 second
256 megabytes
standard input
standard output
Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.
String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.
First line of input contains string s, consisting only of lowercase Latin letters (1 ≤ |s| ≤ 1000, |s| denotes the length of s).
Second line of input contains integer k (1 ≤ k ≤ 26).
Print single line with a minimum number of necessary changes, or the word «impossible» (without quotes) if it is impossible.
yandex
6
0
yahoo
5
1
7
impossible
In the first test case string contains 6 different letters, so we don't need to change anything.
In the second test case string contains 4 different letters: {'a', 'h', 'o', 'y'}. To get 5 different letters it is necessary to change one occurrence of 'o' to some letter, which doesn't occur in the string, for example, {'b'}.
In the third test case, it is impossible to make 7 different letters because the length of the string is 6.
题意:给你一个字符串,问你至少要改变多少个字符才能有k个不同字符?
题解:先看字符串长度是否有k个,然后用一个vis数组标记有多少个不用字符出现(tmp),然后如果k>tmp的话ans=k-tmp,否则为0;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=1e3+;
const ll inf=0x4f4f4f4f4f;
int n,k;
char b[maxn];
int tmp;
bool vis[];
int main()
{
scanf("%s %d",b,&k);
int len=strlen(b);
if(len<k)
{
puts("impossible");return ;
}
for(int i=;i<len;i++)
{
int t=b[i]-'a';
if(!vis[t])vis[t]=true,tmp++;
}
if(k>tmp)
printf("%d\n",k-tmp);
else
printf("0\n");
return ;
}
1 second
256 megabytes
standard input
standard output
You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that:
- All cells in a set have the same color.
- Every two cells in a set share row or column.
The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly.
The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.
Output single integer — the number of non-empty sets from the problem description.
1 1
0
1
2 3
1 0 1
0 1 0
8
In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets.
题解:先从n行上算,后再同理从m列上算,每行算出白的个数tmp,黑的个数就是n-tmp,从中tmp挑出1~tmp个的和是2^tmp-1,但这样挑出一个的会算两次,最后把结果再减去n*m就可以了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn=1e3+;
const ll inf=0x4f4f4f4f4f;
int n,m,k;
int a[][];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>a[i][j];
}
}
ll ans=;
for(int i=;i<n;i++)
{
int tmp=;
for(int j=;j<m;j++)
{
if(a[i][j])tmp++;
}
ans=ans+pow(.,tmp)-+pow(.,m-tmp)-;
}
for(int i=;i<m;i++)
{
int tmp=;
for(int j=;j<n;j++)
{
if(a[j][i])tmp++;
}
ans=ans+pow(.,tmp)-+pow(.,n-tmp)-;
}
cout<<ans-n*m<<endl;
return ;
}
1 second
256 megabytes
standard input
standard output
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
6
3 2 1 6 5 4
4
2 1 3
1 2
2 4 6
1 5
6
83 -75 -49 11 37 62
1
6 1 2 3 4 5 6
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
题意:就是把一个序列分成尽可能多的子序列,使得每子序列排好序之后,整个序列也是拍好序的
题解:把数组储存再a[]和b[]中,后再把b[]排好序找到每个数应该在的位置,再从a[]开始dfs()找调换的位置,调换好的标记,用vector<int>储存答案就好了,具体dfs()过程看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+;
int n,m,tmp;
int a[maxn];
int b[maxn];
map<int,int>mp;
map<int,bool>mp2;
vector<int> ans[maxn];
void dfs(int x,int p)
{
mp2[x]=true;
if(mp[x]==p)
{
ans[tmp++].pb(p);
return;
}
ans[tmp].pb(p);
if(a[mp[x]]==b[p])
{
ans[tmp++].pb(mp[x]);
mp2[b[p]]=true;
return ;
}
if(!mp2[a[mp[x]]])
dfs(a[mp[x]],mp[x]);
else
{
tmp++;
return ;
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(b+,b++n);
for(int i=;i<=n;i++)
{
mp[b[i]]=i;
mp2[b[i]]=false;
}
for(int i=;i<=n;i++)
{
if(!mp2[a[i]])
{
dfs(a[i],i);
}
}
cout<<tmp<<endl;
for(int i=;i<tmp;i++)
{
cout<<ans[i].size()<<' ';
for(int j=;j<ans[i].size();j++)
{
cout<<ans[i][j]<<' ';
}
cout<<'\n';
}
return ;
}
1 second
256 megabytes
standard input
standard output
This is an interactive problem.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti ≠ - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
- ? i (1 ≤ i ≤ n) — ask the values valuei and nexti,
- ! ans — give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
The first line contains three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109) — the number of elements in the list, the index of the first element and the integer x.
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
To make a query of the first type, print ? i (1 ≤ i ≤ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
It is guaranteed that if nexti ≠ - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 ≤ n ≤ 50000, 1 ≤ start ≤ n, 0 ≤ x ≤ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≤ valuei ≤ 109, - 1 ≤ nexti ≤ n, nexti ≠ 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
5 3 80
97 -1
58 5
16 2
81 1
79 4
? 1
? 2
? 3
? 4
? 5
! 81
You can read more about singly linked list by the following link: https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list
The illustration for the first sample case. Start and finish elements are marked dark.
题意:交互题,给你一个单向列表这(单调递增)的长度和起始下标,以及一个数x,然后你可以询问不超过2000次某个下标的的值和他下个数的下标,让你找到这这个列表中>=x的最小值
题解:生成一个随机数组,询问前一千个找到其中小于x的最大值和下标,然后再去一个一个询问找到>=x的值,找不到就为-1
srand(time(NULL));random_shuffle(s.begin(),s.end());这两句可以把有序的数组变成随机数组
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+;
int n,m,st;
vector<int>s;
int main()
{
srand(time(NULL));
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n>>st>>m;
int ans=,p=st;
for(int i=;i<=n;i++)
{
s.pb(i);
}
random_shuffle(s.begin(),s.end());
for(int i=;i<min(n,);i++)
{
int v,next;
cout<<"? "<<s[i]<<endl;
cout.flush();
cin>>v>>next;
if(ans<v&&v<=m)
{
ans=v;p=next;
}
}
while(p!=-&&ans<m)
{
cout<<"? "<<p<<endl;
cout.flush();
cin>>ans>>p;
}
if(ans<m)
{
cout<<"! "<<-<<endl;
}
else
{
cout<<"! "<<ans<<endl;
}
cout.flush();
return ;
}
AIM Tech Round 4 (Div. 2)ABCD的更多相关文章
-
codeforce AIM tech Round 4 div 2 B rectangles
2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...
-
AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
-
AIM Tech Round 3 (Div. 2) A B C D
虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...
-
AIM Tech Round 3 (Div. 2) B
Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...
-
AIM Tech Round 3 (Div. 2) A
Description Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Ko ...
-
AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)
rating又掉下去了.好不容易蓝了.... A..没读懂题,wa了好几次,明天问队友补上... B. Checkpoints 题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n ...
-
AIM Tech Round 3 (Div. 2) B 数学+贪心
http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...
-
AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)
D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...
-
AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
随机推荐
-
[转]extjs组件添加事件监听的三种方式
原文地址:http://blog.csdn.net/y6300023290/article/details/18989635 1.在定义组件配置的时候设置 xtype : 'textarea', na ...
-
jquery加入购物车飞入的效果
主要原理是:点击当前图片的时候,复制(克隆)当前图片在当前位置,然后利用jQuery的animate()方法实现图像的飞入效果 效果预览:http://runjs.cn/detail/qmf0mtm1 ...
-
mybatis至mysql插入一个逗号包含值误差
mybatis至mysql插入形如"11,22,33"当误差.我使用了错误的原因是美元符号镶嵌sql.正确的做法是使用# 有时间去看看mybatis的$和#差异. 版权声明:本文 ...
-
Spring AOP失效之谜
每天学习一点点 编程PDF电子书免费下载: http://www.shitanlife.com/code 什么是AOP1 AOP(Aspect Oriented Programming),即面向切面编 ...
-
sed 简明教程 (转)
sed 简明教程 2013年2月20日 awk于1977年出生,今年36岁本命年,sed比awk大2-3岁,awk就像林妹妹,sed就是宝玉哥哥了.所以 林妹妹跳了个Topless,他的哥哥sed ...
-
android手机平板如何使用usb有线网卡
最近有个项目需要在android平板上使用usb有线网卡,所以做了一部分工作,在这里简单总结一下. 我在TB上购买了一个micro-usb接口的android免驱有线网卡,这个网上很多,随便买一个符合 ...
-
hello word 应用程序的编写
1.各类文件的书写 src中的文件: hello文件夹中的Makefile文件 # # Copyright (C) - OpenWrt.org # # This is free software, l ...
-
【Java并发编程】:深入Java内存模型——happen-before规则及其对DCL的分析
happen—before规则介绍 Java语言中有一个“先行发生”(happen—before)的规则,它是Java内存模型中定义的两项操作之间的偏序关系,如果操作A先行发生于操作B,其意思就是说, ...
-
工具_HBuilder工具使用技巧
https://www.cnblogs.com/xiaohouzai/p/7696152.html
-
【原创】PHPstorm本地修改同步保存到远程服务器
PHPstorm设置本地修改的代码同步保存到远程服务器: 设置里面搜索“Deployment”,选择+号,然后选择SFTP: 填写远程主机的信息: 然后选择Mappings,填写本地代码路径和远程主机 ...