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. 数据库与数据仓库的比较Hbase——Hive

    数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non-Volatile).反映历史变化(Time Varian ...

  2. 字符串拆分和拼接(含list拼接)---基于python

    最近得一超长字符串如下: l=“5245474953544552207369703a3137322e3136312e31302e323232205349502f322e300d0a5669613a20 ...

  3. SQLSERVER 根据身份证号码 往出生年月日 赋值

    update CREW_SailorInfo set DT_DOB= ( case then , ) then , ) else null end) 注:此问题仅供参考 如有疑问 请加QQ群18153 ...

  4. Beta周第8次Scrum会议(11/17)【王者荣耀交流协会】

    一.小组信息 队名:王者荣耀交流协会 小组成员 队长:高远博 成员:王超,袁玥,任思佳,王磊,王玉玲,冉华 小组照片 今天拍照的人是王磊.因此他没有出现在照片中. 二.开会信息 时间:2017/11/ ...

  5. 冲刺ing-6

    第六次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 Leangoo的看板截图,燃尽图 蔺皓雯 编写博客,界面设计 蔡晨旸 界面设计 曾茜 测试 鲁婧楠 学习后端设计 杨池宇 界面设计 成员遇 ...

  6. Java 静态代码块&构造代码块&局部代码块

    /* 静态代码块. 随着类的加载而执行.而且只执行一次. 作用: 用于给类进行初始化. */ class StaticCode { static int num ; static { num = 10 ...

  7. 整理sql server数据类型

    我们在平常开发过程中,在设计数据的时候,经常碰到数据类型选择的问题,为了更快,更合适地选择正确的数据类型,所以在这里做个总结. 分类 sql server 数据类型 c# 数据类型 描述 应用场景 字 ...

  8. jQuery之offset,position

    获取/设置标签的位置数据 * offset(): 相对页面左上角的坐标 * position(): 相对于父元素左上角的坐标. 需求: 1. 点击 btn1 打印 div1 相对于页面左上角的位置 打 ...

  9. 2nd 本周例行报告

    每周例行报告 1.个人项目:词频统计更新 C类型 C内容 S开始时间 E结束时间 I间隔(min) T净时间(min) 分析 功能分析 8:30 10:00 20 70 学习 查阅资料 10:00 1 ...

  10. Jmeter 快速入门--简单的http压测

    1.添加线程组 打开jmeter主窗口后,选择左侧树形结构里的"测试计划",然后右键选择添加,选择"threads(users)",选择"线程组&qu ...