Codeforces Round #410 (Div. 2) 部分解析

来源:互联网 发布:物理数据库模型图 编辑:程序博客网 时间:2024/05/07 20:48

第一次打cf的比赛,谈一谈收货,写一写题解,首先代码有错的时候,如果觉得自己的算法是对的的,那么就去想一想有没什么特殊情况,或者其他·情况没考虑到,如果还是wa了

并且觉得自己所有情况都考虑到了,那么就去看一看是不是题目读错了,如果还是错那应该是算法设计错了,或者还是第一步那里出了问题,最后不到不得已不看要测试数据

好  下面是前三题题解

A:

A. Mike and palindrome
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first and single line contains string s (1 ≤ |s| ≤ 15).

Output

Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.

Examples
input
abccaa
output
YES
input
abbcca
output
NO
input
abcda
output
YES

题目大意:

一开始读错题了,就是他必须修改一个值,这就很气,读对题意后,还是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:

B. Mike and strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

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.

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

Examples
input
4xzzwozwoxzzzwoxxzzwo
output
5
input
2molzvlzvmo
output
2
input
3kckckc
output
0
input
3aaaaab
output
-1
Note

In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".

这道题思路也没错,题也读对了,就是有情况没考虑到,就是当只有一个字符串的时候,尴尬了,按照我的做法就输出-1了,思路还是比较简单的,暴力就行

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:

C. Mike and gcd problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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

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.

Examples
input
21 1
output
YES1
input
36 2 4
output
YES0
input
21 3
output
YES1
Note

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.

这道题完全没思路啊,只能看题解了,首先我们对一开始的数列检验一遍gcd是否为大于1,大于1就输出yes 0,否则的话,就需要变化了,重点来了,题目说可以吧x,y,变为x-y,x+y,这样一定是有特殊原因的,我们不妨设gcd(x,y)==1,d = gcd(x-y,x+y),这时候 d|x-y,d|x+y,又因为当d|a,d|b时,d|(at+bs),t,s为任意整数,那么我们就可以得出

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;        }    }}


0 0
原创粉丝点击