Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome
Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
The first and single line contains string s (1 ≤ |s| ≤ 15).
Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
abccaa
YES
abbcca
NO
abcda
YES
题目链接:http://codeforces.com/contest/798/problem/A
题意:
改变一个字符,让这组字符串变成回文序列,能的话输出YES,不能输出NO
分析:
这道题有点坑,应该是卡了三组数据,aa输出NO,a输出YES,aba也输出YES,估计过了这三组,就可以AC了吧
这题我的做法是分奇偶性,判断条件s[i]==s[len-i-1]是否成立,成立的话ans++,然后判断奇数项长度ans==(len)/2-1||ans==len/2是否成立,成立输出YES,否则输出NO
偶数项长度只需判断ans==(len)/2-1是否成立,成立为YES,否则为NO
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[];
while(cin>>s)
{
int len=strlen(s);
for(int i=;i<len;i++)
{
for(int j='a';j<='z';j++)
{
if(s[i]==j)
continue;
}
}
int ans=;
if(len%!=)
{
for(int i=;i<(len)/;i++)
{
if(s[i]==s[len-i-])
ans++;
}
if(ans==(len)/-||ans==len/)
cout<<"YES"<<endl;
else cout<<"NO"<<endl; }
else if(len%==)
{
for(int i=;i<(len)/;i++)
{
if(s[i]==s[len-i-])
ans++;
}
if(ans==(len)/-)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return ;
}
B. Mike and strings
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.
This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题目链接:http://codeforces.com/contest/798/problem/B
题意:
枚举一个字符串为标准,求答案,找最小
分析:
调用一个substr函数,具体函数详解请参看http://www.cnblogs.com/ECJTUACM-873284962/p/6763801.html
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
void change(string &s)
{
s=s.substr()+s.substr(,);//字符串截取函数,这里的目的就是一个一个换字符,找最小次数
}
int main()
{
int n;
while(cin>>n)
{
string s[n];
for(int i=;i<n;i++)
cin>>s[i];
int ans=1e+;
int sum;
for(int i=;i<n;i++)
{
sum=;
for(int j=;j<n;j++)
{
string t=s[j];
while(t!=s[i])
{
change(t);
sum++;
if(sum>)
{
ans=-;
break;
}
}
}
ans=min(ans,sum);
}
cout<<ans<<endl;
}
return ;
}
C. Mike and gcd problem
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.
is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
In the first example you can simply make one move to obtain sequence [0, 2] with .
In the second example the gcd of the sequence is already greater than 1.
题目链接:http://codeforces.com/contest/798/problem/C
先对本身所有数求gcd,如果有公共因子则已经成立,不然就按照上述规则,将所有数变为偶数。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e+;
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int a[maxn];
int main()
{
int n,i;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
int gccd=a[];
for(i=;i<n;i++)
{
gccd=gcd(gccd,a[i]);
if(gccd==)
break;
}
if(i==n)
{
cout<<"YES"<<endl<<<<endl;
return ;
}
else
{
int c=,cnt=;
a[n]=;
for(i=;i<=n;i++)
{
if(a[i]%!=)
cnt++;//求这组数中有多少个奇数
else
{
c+=(cnt/);
cnt%=;
if(cnt!=)
c+=;//一个奇数要变2次到偶数
cnt=;
}
}
cout<<"YES"<<endl<<c<<endl;
}
return ;
}
D. Mike and distribution
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.
To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to because it will be to easy to find sequence P if he allowed you to select too many elements!
Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.
On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.
On the first line output an integer k which represents the size of the found subset. k should be less or equal to .
On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct.
5 8 7 4 8 3 4 2 5 3 7
3 1 4 5
题目链接:http://codeforces.com/contest/798/problem/D
题意:
给定两个数组大小为n的数组a,b,让你从里面拿k个,使得对于每个数组,拿的元素之和的二倍大于整个数组的元素之和,其中k<=n/2+1
分析:
实在是太套路的一个题目……只描述奇数的情况,偶数随便取一个数之后就变成了奇数的情况。按A排序,取第一个。之后下标2k,2k+1两个数取其中b大的。按这种构造方法得到的即为符合题意的解。
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e+;
struct node
{
int a,b,j;
}p[maxn];
bool cmp(node c,node d)
{
if(c.a!=d.a)
return c.a>d.a;
else return c.b>d.b;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].a);
p[i].j=i;
}
for(int i=;i<=n;i++)
scanf("%d",&p[i].b);;
sort(p+,p++n,cmp);
printf("%d\n",n/+);
printf("%d ",p[].j);
for(int i=;i<=(n-)/;i++)
{
if(p[*i].b>p[*i+].b)
printf("%d ",p[*i].j);
else printf("%d ",p[*i+].j);
}
if(n%==)
printf("%d ",p[n].j);
printf("\n");
}
return ;
}
Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)的更多相关文章
- Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串
A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)
传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...
- Codeforces Round #166 (Div. 2) A. Beautiful Year【暴力枚举/逆向思维/大于当前数且每个位数不同】
A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】
A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Codeforces Round #410 (Div. 2)
Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...
- Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
随机推荐
- (转)为Xcode添加删除行、复制行快捷键
转摘链接:http://www.jianshu.com/p/cc6e13365b7e 在使用eclipse过程中,特喜欢删除一行和复制一行的的快捷键.而恰巧Xcode不支持这两个快捷键,再一次的恰巧让 ...
- tcpdump 命令详解
TCPdump: dump traffic on a network ,根据使用者的定义对网络上的数据包进行截获的一种包分析工具. TCPdump 可以讲网络中传送的数据包的 头信息完全截获下来提供 ...
- Linux目录结构介绍-http://yangrong.blog.51cto.com/6945369/1288072
1.树状目录结构图 2./目录 目录 描述 / 第一层次结构的根.整个文件系统层次结构的根目录. /bin/ 需要在单用户模式可用的必要命令(可执行文件):面向所有用户,例如:cat.ls.cp,和/ ...
- hello world 为什么我们看到学习中有这一句话!!!
Hello World ,中文意思:你好,世界.世界上的第一个程序就是Hello World,由Brian Kernighan创作. Hello, world"程序是指在计算机屏幕上输出“H ...
- override与重载(overload)的区别
重载是相同函数名字.参数或参数类型不同,进行多次承载以适应不同的需要.(orerload)是面向过程的重载. (override)是面向对象的重载.是进行基类中的函数重写.
- deepin系统下部署Python3.5的开发及运行环境
deepin系统下部署Python3.5的开发及运行环境 1 概述 由于最近要学习python接口自动化测试,所以记录一下相关学习经过及经验,希望对大家可以有所帮助. 2 下载 在python官网下载 ...
- UWP 在Xaml中使用cu和fcu资源
之前一直不知道这个,最近看微软的WTS项目更新,在Xaml中定义了 xmlns:fcu ="http://schemas.microsoft.com/winfx/2006/xaml/pres ...
- 微信小程序红包开发 小程序发红包 开发过程中遇到的坑 微信小程序红包接口的
最近公司在开发一个小程序红包系统,客户抢到红包需要提现.也就是通过小程序来给用户发红包. 小程序如何来发红包呢?于是我想到两个方法. 之前公众号开发一直用了的.一个是红包接口,一个是企业支付接口.一开 ...
- 如何高逼格读取Web.config中的AppSettings
http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity 先插句题外话,下版本的ASP.NET貌似 ...
- mac os x使用Git和bitbucket
简而言之,Git是一个分布式的代码版本管理工具.类似的常用工具还有SVN,CVS. Git相比SVN,CVS,最大的特点也是优点在于提供分布式的代码管理.这不是说SVN等不具有该功能,但就目前来看,G ...