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

题意:必须且只更改一个字符 能否使得字符串回文 是则输出“YES”否则输出"NO"
题解:暴力每个字符的更改,并check是否回文

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll __int64
 4 const int N=1e5+10;
 5 char a[105];
 6 int  main()
 7 {
 8     scanf("%s",a);
 9     int len=strlen(a);
10     for(int i=0;i<len;i++)
11     {
12         for(int j='a';j<='z';j++)
13         {
14             if(a[i]==j)
15                 continue;
16             char exm=a[i];
17             a[i]=j;
18             int jishu=0;
19             for(int k=0;k<len/2;k++)
20             {
21                 if(a[k]==a[len-k-1])
22                     jishu++;
23             }
24             if(jishu==(len/2))
25             {
26                 printf("YES
");
27                 return 0;
28             }
29             a[i]=exm;
30         }
31     }
32     printf("NO
");
33     return 0;
34 }
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
4
xzzwo
zwoxz
zzwox
xzzwo
Output
5
Input
2
molzv
lzvmo
Output
2
Input
3
kc
kc
kc
Output
0
Input
3
aa
aa
ab
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".

题意:给你n个串 对字符串有一种操作(将当前的首个字符放到最后) 问对这n个字符串最少要执行多少次操作使得n个串相同 若无法达到则输出-1

题解:首先判断n个串是否能够达到相同状态,任意取一个串生成一个二倍串 判断其他串是否为这个二倍串的子串即可。若能够到达相同状态,则枚举每个二倍串的子串,统计操作次数,取最小值输出。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll __int64
 4 const int N=1e5+10;
 5 char a[100][100];
 6 char b[100];
 7 char c[100];
 8 int n;
 9 map<string,int>mp;
10 map<string,int>ji;
11 int main()
12 {
13     string str,str1,str2;
14     int ans=1000000000;
15     scanf("%d",&n);
16     for(int i=0;i<n;i++){
17         scanf("%s",a[i]);
18         str.assign(a[i]);
19         mp[a[i]]++;
20         }
21     int len=strlen(a[0]);
22     for(int i=0;i<len;i++)
23     {
24         b[i]=a[0][i];
25         b[i+len]=a[0][i];
26     }
27     int jishu=0;
28     for(int i=0;i<n;i++)
29     {
30         if(strstr(b,a[i]))
31             jishu++;
32     }
33     if(jishu!=n)
34       printf("-1
");
35     else
36     {
37         for(int i=0;i<len;i++)
38         {
39             ji.clear();
40             int exm=i;
41             int jishu=len;
42             int w=0;
43             while(jishu--)
44             {
45               c[w]=b[exm];
46               c[w+len]=b[exm];
47               w++;exm++;
48             }
49             int bu=0;
50             int zong=0;
51             for(int j=len;j>=0;j--)
52             {
53                 str.assign(c,j,len);
54                 if(ji[str]==0)
55                 {
56                   zong=zong+bu*mp[str];
57                   bu++;
58                   ji[str]=1;
59                 }
60             }
61             ans=min(ans,zong);
62         }
63         printf("%d
",ans);
64     }
65     return 0;
66 }
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 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).

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
2
1 1
Output
YES
1
Input
3
6 2 4
Output
YES
0
Input
2
1 3
Output
YES
1
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.

题意:给你n个数  一种操作(更改ai, ai + 1   --> ai - ai + 1, ai + ai + 1 )问你最少要执行多少次操作才能使得>1

题解:对 a b执行操作 (a,b)->(a-b,a+b)->(-2b,2a)->(-2b-2a,-2b+2a)->.... 对任意两个数执行最多两次操作即可都变为偶数;

为了满足>1 最小的gcd为2 并且所有偶数的的gcd为2 所以若初始的gcd就是大于1则不需要操作,否则遍历一遍n个数,执行最少的操作使得所有的数都为偶数,注意最后一个数。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll __int64
 4 const int N=1e5+10;
 5 int n;
 6 ll a[N];
 7 int main()
 8 {
 9     scanf("%d",&n);
10     scanf("%I64d",&a[1]);
11     scanf("%I64d",&a[2]);
12     ll zong=__gcd(a[1],a[2]);
13     for(int i=3; i<=n; i++)
14     {
15         scanf("%I64d",&a[i]);
16         zong=__gcd(zong,a[i]);
17     }
18     if(zong>1)
19     {
20         printf("YES
0
");
21         return 0;
22     }
23     ll ans=0;
24     ll aa,bb;
25     ll aaa,bbb;
26     for(int i=1; i<n; i++)
27     {
28         if(a[i]%2==0)
29             continue;
30         aa=a[i]-a[i+1];
31         bb=a[i]+a[i+1];
32         ans++;
33         if(aa%2==0)
34         {
35             a[i+1]=bb;
36             continue;
37         }
38         aaa=aa-bb;
39         bbb=aa+bb;
40         ans++;
41         if(aaa%2==0)
42         {
43             a[i+1]=bbb;
44             continue;
45         }
46     }
47     int flag=0;
48     if(a[n]%2==1)
49     {
50         aa=a[n-1]-a[n];
51         bb=a[n-1]+a[n];
52         ans++;
53         if(aa%2==0&&bb%2==0)
54         {
55             flag=1;
56         }
57         if(flag==0)
58         {
59             ans++;
60         }
61     }
62     printf("YES
");
63     printf("%I64d
",ans);
64     return 0;
65 }
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 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!

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

Example
Input
5
8 7 4 8 3
4 2 5 3 7
Output
3
1 4 5
题意:给你长度为n的a,b数组 在a,b数组的相同位置取出
个数 使得a数组中取出的数的和的2倍大于a数组的和 并且b数组中取出的数的和的2倍大于b数组的和
题解:排序姿势/随机姿势/orzzzz
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 struct node
 5 {
 6     int x;
 7     int y;
 8     int id;
 9 }N[100005];
10 int check[100005];
11 bool  cmp( struct node aa,struct node bb)
12 {
13     return aa.x>bb.x;
14 }
15 int main()
16 {
17     scanf("%d",&n);
18     memset(N,0,sizeof(N));
19     memset(check,0,sizeof(check));
20     for(int i=1;i<=n;i++)
21         scanf("%d",&N[i].x);
22     for(int i=1;i<=n;i++)
23         scanf("%d",&N[i].y);
24     for(int i=1;i<=n;i++)
25         N[i].id=i;
26     sort(N+1,N+1+n,cmp);
27     int k;
28     if(n%2==0)
29         k=1;
30     else{
31         k=2;
32         }
33     for(;k<=n;k+=2)
34     {
35         if(N[k].y>N[k+1].y)
36             check[N[k].id]=1;
37         else
38             check[N[k+1].id]=1;
39     }
40     for(int i=1;i<=n;i++)
41     {
42         if(check[N[i].id]==0)
43         {
44             check[N[i].id]=1;
45             break;
46         }
47     }
48     printf("%d
",n/2+1);
49     for(int i=1;i<=n;i++)
50     {
51         if(check[N[i].id])
52             printf("%d ",N[i].id);
53     }
54     printf("
");
55     return 0;
56 }

 //随机写法

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 long long a[100005],b[100005],c[100005];
 4 int main ( ) {
 5     long long n,i,k,sa=0,sb=0;
 6     scanf("%I64d",&n);
 7     k=n/2+1;
 8     for (i=1;i<=n;i++) scanf("%I64d",&a[i]),c[i]=i,sa+=a[i];
 9     for (i=1;i<=n;i++) scanf("%I64d",&b[i]),sb+=b[i];
10     while (true) {
11         bool f=true;
12         long long s1=0,s2=0;
13         for (i=1;i<=k;i++) {
14             s1+=a[c[i]];s2+=b[c[i]];
15         }
16         if (s1*2>sa && s2*2>sb) {
17             printf("%d
",k);
18             for (i=1;i<=k;i++) printf("%d ",c[i]);
19             return 0;
20         }
21         random_shuffle(c+1,c+n+1);
22     }
23     return 0;
24 }

使得a数组中取出的数的和的2倍大于a数组的和

原文地址:https://www.cnblogs.com/hsd-/p/6749441.html