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. HWI的安装

    一.安装的过程 hwi的安装过程: 1.解压src源码包:tar -zvxf apache-hive-1.2.2-src.tar.gz 2.进到HWI目录下:cd /home/bigdata/apac ...

  2. 高可用Kubernetes集群-9. 部署kubelet

    十一.部署kubelet 接下来两个章节是部署Kube-Node相关的服务,包含:kubelet,kube-proxy. 1. TLS bootstrap用户授权 # kubelet采用TLS Boo ...

  3. Action Required: Listings Deactivated for Potential Pricing Error

    Dear Seller, We are contacting you because we have detected potential pricing errors in your Amazon. ...

  4. “Hello World!”团队第六周的第二次会议

    今天是我们团队“Hello World!”团队第六周召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  5. Struts2(七)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.xml验证 Struts2提供了验证器,实现了通用的验证逻辑.例如: 非空验证器.长度验证器.日期验证器.email验证器等.具体定 ...

  6. 【Mark】Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  7. 解决pciss_spc导入提示表空间不存在以及扩展失败的问题

    select NAME FROM USER$ ORDER BY NAME ; CREATE USER pciss IDENTIFIED BY pciss ; GRANT DBA TO pciss ; ...

  8. Python @retry装饰器的使用与实现案例(requests请求失败并重复请求)

    在爬虫代码的编写中,requests请求网页的时候常常请求失败或错误,一般的操作是各种判断状态和超时,需要多次重试请求,这种情况下,如果想优雅的实现功能,可以学习下retrying包下的retry装饰 ...

  9. pixi.js tools

    pixi群 881784250 Awesome pixi.js tools A list of useful libs/resources/tools for renowned html5 rende ...

  10. Ubuntu系统下adb devices 不能显示手机设备

    1. 查看usb设备,命令:lsusb 结果如下:Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub B ...