Codeforces Round #410

来源:互联网 发布:对高维数据进行降维 编辑:程序博客网 时间:2024/05/16 19:44
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 tochange 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 changeexactly 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

题意大致是给定一个字符串,判断改变一个字符是否恰好能把它变成回文串,每次必须改变一个字符。其实只要判断一下字符串中线两侧有几对不一样的字符即可。如果恰好是一对,则满足题目要求,否则输出NO。这里有一个坑,如果字符串本来就是回文串的话,长度是奇数才满足要求。代码如下:

#include<iostream>#include<string>using namespace std;int main(){    string a;    cin>>a;    int t = a.length() - 1;    int p = t + 1;    int sum = 0;    for(int i = 0; 2 * i <= t ; ++i)    {        if(a[i] != a[t - i]) ++sum;    }    if(sum == 1 || (p & 1) && sum == 0) cout << "YES" << endl;    else cout << "NO" << endl;    return 0;}
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 stringss1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a stringsi, 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. Thei-th line corresponding to stringsi. Lengths of strings are equal. Lengths of each string is positive and don't exceed50.

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

当时没想出来以为是什么玄学的做法,题解竟然是暴力枚举每个串作为终点再见,不说了。。。贴代码:
#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;char a[55][55],b[110];int n;int Find(int t,int le){    for(int i = 0;i < 2 * le; ++i)    {        if(b[i] == a[t][0])        {            int j = i,k = 0;            while(a[t][k] == b[j] && j < 2*le && k < le) ++j,++k;            if(k == le) return i;        }    }    return -1;}int cal(int t,int le){    int sum = 0;    for(int i = 0;i < n; ++i)    {        if(i == t) continue;        strcpy(b,a[i]),strcpy(b+le,a[i]);        sum += Find(t,le);    }    return sum;}int main(){    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    scanf("%d",&n);    for(int i = 0;i < n; ++i) scanf(" %s",a[i]);    int len = strlen(a[0]);    strcpy(b,a[0]),strcpy(b+len,a[0]);    for(int i = 0;i < n; ++i)        if(Find(i,len) < 0)        {            printf("-1\n");            return 0;        }    int ans = 1 << 30;    for(int i = 0;i < n; ++i)    {        ans = min(ans,cal(i,len));    }    printf("%d\n",ans);    return 0;}
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 lengthn. He considers the sequence B = [b1, b2, ..., bn] beautiful if thegcd 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 indexi (1 ≤ i < n), delete numbersai, ai + 1 and put numbersai - 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 sequenceA beautiful if it's possible, or tell him that it is impossible to do so.

is the biggest non-negative numberd such that d dividesbi for everyi (1 ≤ i ≤ n).

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequenceA.

The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequenceA.

Output

Output on the first line "YES" (without quotes) if it is possible to make sequenceA 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 sequenceA 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 than1.

这道题的思路很巧妙,只要把序列全部变成偶数即可满足gcd大于1。一次move类似一次反转:一次操作可以把两个连续的奇数变成偶数;如果是一个奇数和一个偶数相邻,那么经过两次move,可以把两个数都变成偶数。所以只需要统计连续奇数的个数和单个分布的奇数的个数即可,其他处理详见代码。需要注意的是,进行统计之前,需要预判一下序列是否已满足gcd大于1,暴力判断即可,对。。。暴力判断。代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1e5 + 5;int a[maxn];int gcd(int a,int b){    return b == 0 ? a : gcd(b,a % b);}int main(){    int n,ans = 0,g;    memset(a,0,sizeof(a));    scanf("%d",&n);    for(int i = 0;i < n; ++i)    {        scanf("%d",&a[i]);        if(i == 0) g = a[i];        else g = gcd(a[i],g);    }    printf("YES\n");    if(g > 1) printf("0\n");    else    {        for(int i = 0;i < n; ++i)        {            if(a[i]&1)            {                int t = 0;                while(a[i] & 1 && i < n) ++t,++i;                ans += (t / 2);                if(t & 1) ans += 2;            }        }        printf("%d\n",ans);    }    return 0;}
D. Mike and distribution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integersA = [a1, a2, ..., an] andB = [b1, b2, ..., bn] of lengthn 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 selectk numbers P = [p1, p2, ..., pk] such that1 ≤ pi ≤ n for1 ≤ 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 subsetP "unfair" if and only if the following conditions are satisfied:2·(ap1 + ... + apk) isgreater than the sum of all elements from sequenceA, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequenceB. Also, k should be smaller or equal to because it will be to easy to find sequenceP 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!

Input

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.

Output

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 integersp1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequenceP should be distinct.

Example
Input
58 7 4 8 34 2 5 3 7
Output
31 4 5
从一开始就想歪了。。。看了大神的博客只能说666,这其实是一道贪心题,给a数组按数值大小从大到小排序。数组长度为奇数时,为保证选够n/2 + 1个,所以第一个必选,后面的两个一对,按p(即序号)选择b数组下数值较大的一个,可得到合理答案,可证明。代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef struct node{    int p,x;}node;bool cmp(const node x,const node y){    return x.x > y.x;}const int maxn = 1e5 + 5;node a[maxn],b[maxn];int c[maxn];int main(){    int n;    scanf("%d",&n);    for(int i = 1;i <= n; ++i) scanf("%d",&a[i].x),a[i].p = i;    for(int i = 1;i <= n; ++i) scanf("%d",&b[i].x),b[i].p = i;    int t = 0;    sort(a + 1,a + n + 1,cmp);    c[t++] = a[1].p;    for(int i = 2;i <= n;i += 2)    {        if(i == n) c[t++] = a[n].p;        else        {            if(b[a[i].p].x > b[a[i + 1].p].x) c[t++] = a[i].p;            else c[t++] = a[i + 1].p;        }    }    printf("%d\n",t);    for(int i = 0;i < t; ++i) printf("%d ",c[i]);    printf("\n");    return 0;}

0 0