Codeforces Round #410 (Div. 2) 部分解析
来源:互联网 发布:物理数据库模型图 编辑:程序博客网 时间:2024/05/07 20:48
第一次打cf的比赛,谈一谈收货,写一写题解,首先代码有错的时候,如果觉得自己的算法是对的的,那么就去想一想有没什么特殊情况,或者其他·情况没考虑到,如果还是wa了
并且觉得自己所有情况都考虑到了,那么就去看一看是不是题目读错了,如果还是错那应该是算法设计错了,或者还是第一步那里出了问题,最后不到不得已不看要测试数据
好 下面是前三题题解
A:
一开始读错题了,就是他必须修改一个值,这就很气,读对题意后,还是wa了,就是有一种情况没考虑到,当长度为奇数时,如果他一开始就是回文串,那么也是满足的,因为我们修改一下中间那个点就行了。
ac代码:
#include<cstdio>#include<iostream>#include<cstring>#include<map>#include<sstream>using namespace std;int main(){ string a; while(cin>>a) { int flag = 1; int len = a.size(); int ok = 1; if(len&1) { int len2 = len/2; int mid = len2; for(int i = 1; i <= len2 ;i++) { if(a[mid-i] != a[mid+i]) { if(flag) flag = 0; else { ok = 0; break; } } } flag = 0; } else { int len2 = len/2; int start = len2; for(int i = 1 ; i <= len2;i++) { if(a[start-i] != a[start+i-1]) { if(flag) flag = 0; else { ok = 0; break; } } } } if(ok&&flag==0) puts("YES"); else puts("NO"); } return 0;}
B:
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.
4xzzwozwoxzzzwoxxzzwo
5
2molzvlzvmo
2
3kckckc
0
3aaaaab
-1
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
ac代码:
#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<map>#include<vector>#include<string>#include<sstream>using namespace std;string s[55];int vis[500];int n;int main(){ while(~scanf("%d",&n)) { int p = 0; memset(vis,0,sizeof(vis)); for(int i = 0;i<n;i++) { cin>>s[i]; } int falg_len = s[0].size(); string falg[55]; falg[0] = s[0]; for(int i = 1;i<falg_len;i++) { falg[i] = falg[i-1].substr(1,falg_len); falg[i] += falg[i-1][0]; } int tmp_ans = 0; int ans = 0x3f3f3f3f; int ok = 0; if(n==1) { cout<<0<<endl; } else{ for(int i = 0; i < falg_len; i++) { tmp_ans = i; ok = 0; //cout<<1<<':'<<falg[i]<<endl; for(int j = 1;j < n ;j++) { ok = 0; int len = s[j].size(); for(int k = 0;k < len;k++) { if(s[j][k] == falg[i][0]) { int start = k; string tmp = s[j].substr(start,len); for(int w = 0;w <= k-1;w++) { tmp += s[j][w]; } if(tmp == falg[i]) { //cout<<"j:"<<j<<' '<<tmp<<endl; tmp_ans += start; ok = 1; break; } } } if(!ok) { break; } } if(ok) { ans = min(tmp_ans,ans); } } if(ans == 0x3f3f3f3f) { cout<<-1<<endl; } else cout<<ans<<endl; } } return 0;}
C:
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 numbersai, 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.
21 1
YES1
36 2 4
YES0
21 3
YES1
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.
d|2x,d|2y,所以 d|2(x+y) -> d|2 变换后的gcd能整除2 所以,其他的一定要是偶数,那么这道题就变为把所有数变为偶数的最小次数,接下来的数列中相邻的两个数就是只有三种情况,奇奇,奇偶,偶偶,第三种不用管它,然后我们先处理第一种,在处理第二种,那么问题来了,为什么要先处理第一种,如果先处理第二种,那么如果一个数列全是奇数的情况就不行。
ac代码:
#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<map>#include<iostream>#define LL long longusing namespace std;LL a[100000+5];int n;LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}int main(){ while(~scanf("%d",&n)) { for(int i = 0;i<n;i++) { scanf("%I64d",&a[i]); } LL tmp = gcd(a[0],a[1]); //cout<<tmp<<endl; for(int i = 2;i<n;i++) { tmp = gcd(tmp,a[i]); //cout<<tmp<<endl; } if(tmp>1) { cout<<"YES"<<endl; cout<<0<<endl; } else { LL ans = 0; for(int i = 0;i<n-1;i++) { if((a[i]&1)&&(a[i+1]&1)) { ans++; a[i] = 0; a[i+1] = 0; } } for(int i = 0;i < n-1;i++) { if(((a[i]&1)&&(a[i+1]%2==0))||((a[i]%2==0)&&(a[i+1]&1))) { ans+=2; a[i] = 0; a[i+1] = 0; } } cout<<"YES"<<endl; cout<<ans<<endl; } }}
- Codeforces Round #410 (Div. 2) 部分解析
- Codeforces Round #216 (Div. 2)部分题解
- Codeforces Round #277.5 (Div. 2)部分题解
- Codeforces Round #359 (Div. 2) 部分题解
- Codeforces Round #378 (Div. 2) 部分题解
- Codeforces Round #386 (Div.2)部分题解
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #181 (Div. 2) 部分解题报告
- codeforces Round 211 Div 2部分题解(持续更新中.......)
- Codeforces Round #227 (Div. 2)(部分题解)
- Codeforces Round #377 (Div. 2)部分题解A+B+C!
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) 部分题解
- 【解题报告】Codeforces Round #410 (Div. 2)
- 【Codeforces Round #410 (Div. 2) B】暴力
- Codeforces Round #410 (Div. 2) 解题报告
- 4.28练习
- 51nod 1009 数字1的数量(思维)
- 《Cracking the Coding Interview程序员面试金典》----链表分割
- # Python数据分析学习笔记(一)
- 每日三题-Day4-C(HDU 1159 Common Subsequence 最长公共子序列O(nlogn)解法)
- Codeforces Round #410 (Div. 2) 部分解析
- Nginx实现对chunk请求支持
- HDFS 基本使用
- YUV420图像的裁剪
- 老年代碎片化问题. java5的改进.
- Tensorflow实现线性回归
- matlab中all和any用法
- Java允许使用被禁止的类
- 1038. 统计同成绩学生