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是否回文
 #include<bits/stdc++.h>
using namespace std;
#define ll __int64
const int N=1e5+;
char a[];
int main()
{
scanf("%s",a);
int len=strlen(a);
for(int i=;i<len;i++)
{
for(int j='a';j<='z';j++)
{
if(a[i]==j)
continue;
char exm=a[i];
a[i]=j;
int jishu=;
for(int k=;k<len/;k++)
{
if(a[k]==a[len-k-])
jishu++;
}
if(jishu==(len/))
{
printf("YES\n");
return ;
}
a[i]=exm;
}
}
printf("NO\n");
return ;
}
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个串是否能够达到相同状态,任意取一个串生成一个二倍串 判断其他串是否为这个二倍串的子串即可。若能够到达相同状态,则枚举每个二倍串的子串,统计操作次数,取最小值输出。

 #include<bits/stdc++.h>
using namespace std;
#define ll __int64
const int N=1e5+;
char a[][];
char b[];
char c[];
int n;
map<string,int>mp;
map<string,int>ji;
int main()
{
string str,str1,str2;
int ans=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",a[i]);
str.assign(a[i]);
mp[a[i]]++;
}
int len=strlen(a[]);
for(int i=;i<len;i++)
{
b[i]=a[][i];
b[i+len]=a[][i];
}
int jishu=;
for(int i=;i<n;i++)
{
if(strstr(b,a[i]))
jishu++;
}
if(jishu!=n)
printf("-1\n");
else
{
for(int i=;i<len;i++)
{
ji.clear();
int exm=i;
int jishu=len;
int w=;
while(jishu--)
{
c[w]=b[exm];
c[w+len]=b[exm];
w++;exm++;
}
int bu=;
int zong=;
for(int j=len;j>=;j--)
{
str.assign(c,j,len);
if(ji[str]==)
{
zong=zong+bu*mp[str];
bu++;
ji[str]=;
}
}
ans=min(ans,zong);
}
printf("%d\n",ans);
}
return ;
}
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个数,执行最少的操作使得所有的数都为偶数,注意最后一个数。

 #include<bits/stdc++.h>
using namespace std;
#define ll __int64
const int N=1e5+;
int n;
ll a[N];
int main()
{
scanf("%d",&n);
scanf("%I64d",&a[]);
scanf("%I64d",&a[]);
ll zong=__gcd(a[],a[]);
for(int i=; i<=n; i++)
{
scanf("%I64d",&a[i]);
zong=__gcd(zong,a[i]);
}
if(zong>)
{
printf("YES\n0\n");
return ;
}
ll ans=;
ll aa,bb;
ll aaa,bbb;
for(int i=; i<n; i++)
{
if(a[i]%==)
continue;
aa=a[i]-a[i+];
bb=a[i]+a[i+];
ans++;
if(aa%==)
{
a[i+]=bb;
continue;
}
aaa=aa-bb;
bbb=aa+bb;
ans++;
if(aaa%==)
{
a[i+]=bbb;
continue;
}
}
int flag=;
if(a[n]%==)
{
aa=a[n-]-a[n];
bb=a[n-]+a[n];
ans++;
if(aa%==&&bb%==)
{
flag=;
}
if(flag==)
{
ans++;
}
}
printf("YES\n");
printf("%I64d\n",ans);
return ;
}
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
 #include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
int x;
int y;
int id;
}N[];
int check[];
bool cmp( struct node aa,struct node bb)
{
return aa.x>bb.x;
}
int main()
{
scanf("%d",&n);
memset(N,,sizeof(N));
memset(check,,sizeof(check));
for(int i=;i<=n;i++)
scanf("%d",&N[i].x);
for(int i=;i<=n;i++)
scanf("%d",&N[i].y);
for(int i=;i<=n;i++)
N[i].id=i;
sort(N+,N++n,cmp);
int k;
if(n%==)
k=;
else{
k=;
}
for(;k<=n;k+=)
{
if(N[k].y>N[k+].y)
check[N[k].id]=;
else
check[N[k+].id]=;
}
for(int i=;i<=n;i++)
{
if(check[N[i].id]==)
{
check[N[i].id]=;
break;
}
}
printf("%d\n",n/+);
for(int i=;i<=n;i++)
{
if(check[N[i].id])
printf("%d ",N[i].id);
}
printf("\n");
return ;
}

//随机写法

 #include<bits/stdc++.h>
using namespace std;
long long a[],b[],c[];
int main ( ) {
long long n,i,k,sa=,sb=;
scanf("%I64d",&n);
k=n/+;
for (i=;i<=n;i++) scanf("%I64d",&a[i]),c[i]=i,sa+=a[i];
for (i=;i<=n;i++) scanf("%I64d",&b[i]),sb+=b[i];
while (true) {
bool f=true;
long long s1=,s2=;
for (i=;i<=k;i++) {
s1+=a[c[i]];s2+=b[c[i]];
}
if (s1*>sa && s2*>sb) {
printf("%d\n",k);
for (i=;i<=k;i++) printf("%d ",c[i]);
return ;
}
random_shuffle(c+,c+n+);
}
return ;
}

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

Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机的更多相关文章

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

  2. Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)

    传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...

  3. Codeforces Round #410 (Div. 2)

    Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...

  4. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  5. Codeforces Round #410 (Div. 2) A. Mike and palindrome

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

  6. Codeforces Round #410 (Div. 2) A

    Description Mike has a string s consisting of only lowercase English letters. He wants to change exa ...

  7. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

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

  8. Codeforces Round #410 (Div. 2)D题

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #410 (Div. 2)C题

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

随机推荐

  1. EasyUI validatebox 自定义ajax验证用户名是否已存在

    <td><input type="text" id="userName" name="userName" class=&q ...

  2. c++ Dynamic Memory (part 1)

    1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ...

  3. 【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?(转)

    摘自https://blog.csdn.net/jiange_zh/article/details/50198097 在算法竞赛中,我们常常需要用到一个“无穷大”的值,对于我来说,大多数时间我会根据具 ...

  4. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  5. HDU 1277 Nested Dolls

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1677 题意: 玩俄罗斯套娃,问最后至少还剩几个. 题解: 这题可以和拦截导弹做对比,因为这里是二维的 ...

  6. HDU 5211 Mutiple 水题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5211 题解: 1.筛法: #include<iostream> #include< ...

  7. WebSphere Application Server诊断和调优

    近段时间,我们项目中用到的WebSphere应用服务器(WAS),但在客户的production环境下极不稳定,经常宕机.给客户造成非常不好的影响,同时,也给项目组很大压力.为此,我们花了近一个月时间 ...

  8. 使用kdump内核调试工具遇到的问题及解决

    修改linux内核代码或者内核模块的时候,搞不好就会造成linux死机崩溃,crash死机后/var/log/kern.log里面不会有任何异常信息记录.这时候kdump就会派上用场了,网上kdump ...

  9. OSG学习:阴影代码示例

    效果图: 代码示例: #include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #i ...

  10. Eureka Server Replicate

    为了方便说明,就把上篇博客的图再贴一遍了. 上篇说道Application Service向Eureka Server注册服务的过程,在完成注册之后,由于Eureka Server是对等集群,其他Se ...