Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机
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数组的和
Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机的更多相关文章
- 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 ...
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...
- Codeforces Round #410 (Div. 2)
Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...
- 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 ...
- 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 ...
- Codeforces Round #410 (Div. 2) A
Description Mike has a string s consisting of only lowercase English letters. He wants to change exa ...
- 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 ...
- Codeforces Round #410 (Div. 2)D题
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
随机推荐
- Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]
<!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" cl ...
- CQOI2018 游记 再见OI,既是反思,也是祝福
哎,怎么说呢? 时运不齐,命途多舛? 从头开始说吧. 今年的NOIP大家考的都不尽人意,每个人都有或多或少的失误,全部都几十分几十分地丢.最后大家剩下的觉得可能冲击一下省队的人一共只有7个. 伙伴们变 ...
- 求1到N(正整数)之间1出现的个数
一.题目要求 给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 要求: 写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数.例如 f(12) = 5 ...
- python 动态获取当前运行的类名和函数名的方法
一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp
题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...
- C#高级编程 (第六版) 学习 第四章:继承
第四章 继承 1,继承的类型 实现继承: 一个类派生于一个基类型,拥有该基类型所有成员字段和函数. 接口继承 一个类型只继承了函数的签名,没有继承任何实现代码. 2,实现继承 class MyDe ...
- Google Professional Data Engineer(PDE)考试
在国内参加PDE考试的人比较少,导致资料也很少.我在19年1月30号去上海参加PDE考试,参加前也是完全没底,因为时间短资料少,但幸运的是顺利通过了.回过头来看,其中有些技巧和重点,在此做一些总结,希 ...
- nginx 简介 http://nginx.org
Nginx(一) 官方技术文档网站:http://nginx.org Nginx的特性 1:各功能基于模块化设计,扩展性好 2:支持平滑重启,实现应用不下线部署 3:在多并发请求模型下,内存消 ...
- Kotlin在处理GET和POST请求的数据问题
1.网络请求获取到的数据流处理 java写法 BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8& ...
- python mysql查询结果乱码
在connect()方法中传入charset='utf8'参数即可. conn = MySQLdb.connect(host=get_config_values('mysql', 'host'), p ...