2 seconds
256 megabytes
standard input
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.
The first and single line contains string s (1 ≤ |s| ≤ 15).
Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
abccaa
YES
abbcca
NO
abcda
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 ;
}
2 seconds
256 megabytes
standard input
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?
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.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
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 ;
}
2 seconds
256 megabytes
standard input
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).
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 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.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
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 ;
}
2 seconds
256 megabytes
standard input
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!
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.
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.
5
8 7 4 8 3
4 2 5 3 7
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数组的和