CodeForces Round #550 Div.3
http://codeforces.com/contest/1144
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
The first line contains integer nn (1≤n≤1001≤n≤100), denoting the number of strings to process. The following nn lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 11 and 100100, inclusive.
Print nn lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
8
fced
xyz
r
dabcef
az
aa
bad
babc
Yes
Yes
Yes
Yes
No
No
No
No
代码:
#include <bits/stdc++.h>
using namespace std; int N;
map<char, int> mp; int main() {
scanf("%d", &N);
getchar();
for(int i = ; i < N; i ++) {
mp.clear();
string s;
cin >> s;
bool flag = true;
int len = s.length();
int minn = ;
for(int j = ; j < len; j ++) {
mp[s[j]] ++;
minn = min(minn, s[j] - 'a');
if(mp[s[j]] > ) flag = false;
}
//printf("!!!%d\n", minn);
for(int j = minn; j < minn + len; j ++) {
if(mp[j + 'a'] == ) {
flag = false;
break;
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return ;
}
B. Parity Alternated Deletions
Polycarp has an array aa consisting of nn integers.
He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n−1n−1 elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.
Formally:
- If it is the first move, he chooses any element and deletes it;
- If it is the second or any next move:
- if the last deleted element was odd, Polycarp chooses any even element and deletes it;
- if the last deleted element was even, Polycarp chooses any odd element and deletes it.
- If after some move Polycarp cannot make a move, the game ends.
Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.
Help Polycarp find this value.
The first line of the input contains one integer nn (1≤n≤20001≤n≤2000) — the number of elements of aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤ai≤106), where aiai is the ii-th element of aa.
Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.
5
1 5 7 8 2
0
6
5 1 2 4 6 3
0
2
1000000 1000000
1000000
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + ;
int N;
int num[maxn];
vector<int> odd, even; int main() {
scanf("%d", &N);
for(int i = ; i < N; i ++) {
scanf("%d", &num[i]);
if(num[i] % ) odd.push_back(num[i]);
else even.push_back(num[i]);
}
int ans = ;
sort(odd.rbegin(), odd.rend());
sort(even.rbegin(), even.rend());
int osz = odd.size(), esz = even.size();
if(osz > esz + ) {
for(int i = esz + ; i < osz; i ++)
ans += odd[i];
}
else if(osz == esz + ) ans = ;
else {
for(int i = osz + ; i < esz; i ++)
ans += even[i];
}
printf("%d\n", ans);
return ;
}
standard output
Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing.
Strictly increasing sequence is a sequence of integers [x1<x2<⋯<xk][x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl][y1>y2>⋯>yl]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence aa. After that sequence aa got shuffled. For example, some of the possible resulting sequences aa for an increasing sequence [1,3,4][1,3,4] and a decreasing sequence [10,4,2][10,4,2] are sequences [1,2,3,4,4,10][1,2,3,4,4,10] or [4,2,1,10,4,3][4,2,1,10,4,3].
This shuffled sequence aa is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO".
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.
If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print nini — the number of elements in the strictly increasing sequence. nini can be zero, in this case the increasing sequence is empty.
In the third line print nini integers inc1,inc2,…,incniinc1,inc2,…,incni in the increasing order of its values (inc1<inc2<⋯<incniinc1<inc2<⋯<incni) — the strictly increasing sequence itself. You can keep this line empty if ni=0ni=0 (or just print the empty line).
In the fourth line print ndnd — the number of elements in the strictly decreasing sequence. ndnd can be zero, in this case the decreasing sequence is empty.
In the fifth line print ndnd integers dec1,dec2,…,decnddec1,dec2,…,decnd in the decreasing order of its values (dec1>dec2>⋯>decnddec1>dec2>⋯>decnd) — the strictly decreasing sequence itself. You can keep this line empty if nd=0nd=0 (or just print the empty line).
ni+ndni+nd should be equal to nn and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
7
7 2 7 3 3 1 4
YES
2
3 7
5
7 4 3 2 1
5
4 3 1 5 3
YES
1
3
4
5 4 3 1
5
1 1 2 1 2
NO
5
0 1 2 3 4
YES
0 5
4 3 2 1 0
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int N;
int a[maxn];
int vis[maxn]; int main() {
scanf("%d", &N);
bool flag = true;
for(int i = ; i < N; i ++) {
scanf("%d", &a[i]);
vis[a[i]] ++;
if(vis[a[i]] > ) flag = false;
}
if(!flag) {
printf("NO\n");
return ;
}
sort(a, a + N);
vector<int> up, down;
memset(vis, , sizeof(vis));
for(int i = ; i < N; i ++) {
if(!vis[a[i]]) {
up.push_back(a[i]);
vis[a[i]] = ;
} else down.push_back(a[i]);
}
printf("YES\n");
printf("%d\n", down.size());
if(down.size() == ) printf("\n");
else {
sort(down.begin(), down.end());
for(int i = ; i < down.size(); i ++)
printf("%d%s", down[i], i != down.size() - ? " " : "\n");
}
printf("%d\n", up.size());
if(up.size() == ) printf("\n");
else {
sort(up.rbegin(), up.rend());
for(int i = ; i < up.size(); i ++)
printf("%d%s", up[i], i != up.size() - ? " " : "\n");
}
return ;
}
standard output
You are given an array aa consisting of nn integers. You can perform the following operations arbitrary number of times (possibly, zero):
- Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|ai−aj|ai:=ai+|ai−aj|;
- Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai−|ai−aj|ai:=ai−|ai−aj|.
The value |x||x| means the absolute value of xx. For example, |4|=4|4|=4, |−3|=3|−3|=3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10181018 by absolute value.
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.
In the first line print one integer kk — the minimum number of operations required to obtain the array of equal elements.
In the next kk lines print operations itself. The pp-th operation should be printed as a triple of integers (tp,ip,jp)(tp,ip,jp), where tptp is either 11 or 22 (11means that you perform the operation of the first type, and 22 means that you perform the operation of the second type), and ipip and jpjp are indices of adjacent elements of the array such that 1≤ip,jp≤n1≤ip,jp≤n, |ip−jp|=1|ip−jp|=1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10181018 by absolute value.
If there are many possible answers, you can print any.
5
2 4 6 6 6
2
1 2 3
1 1 2
3
2 8 10
2
2 2 1
2 3 2
4
1 1 1 1
0
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int N;
int a[maxn], vis[maxn];
int st, en, maxx, cnt = ; int main() {
scanf("%d", &N);
for(int i = ; i <= N; i ++) {
scanf("%d", &a[i]);
vis[a[i]] ++;
if(vis[a[i]] > cnt) {
cnt = vis[a[i]];
maxx = a[i];
}
} for(int i = ; i < N; i ++) {
if(a[i] == maxx) {
st = i;
break;
}
} printf("%d\n", N - cnt);
if(N - cnt == ) return ;
for(int i = st; i >= ; i --) {
if(a[i] == maxx) continue;
else if(a[i] > maxx)
printf("2 %d %d\n", i, i + );
else printf("1 %d %d\n", i, i + );
}
for(int i = st; i <= N; i ++) {
if(a[i] == maxx) continue;
else if(a[i] < maxx)
printf("1 %d %d\n", i, i - );
else printf("2 %d %d\n", i, i - );
} return ;
}
You are given two strings ss and tt, both consisting of exactly kk lowercase Latin letters, ss is lexicographically less than tt.
Let's consider list of all strings consisting of exactly kk lowercase Latin letters, lexicographically not less than ss and not greater than tt(including ss and tt) in lexicographical order. For example, for k=2k=2, s=s="az" and t=t="bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].
Your task is to print the median (the middle element) of this list. For the example above this will be "bc".
It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.
The first line of the input contains one integer kk (1≤k≤2⋅1051≤k≤2⋅105) — the length of strings.
The second line of the input contains one string ss consisting of exactly kk lowercase Latin letters.
The third line of the input contains one string tt consisting of exactly kk lowercase Latin letters.
It is guaranteed that ss is lexicographically less than tt.
It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.
Print one string consisting exactly of kk lowercase Latin letters — the median (the middle element) of list of strings of length kklexicographically not less than ss and not greater than tt.
2
az
bf
bc
5
afogk
asdji
alvuw
6
nijfvj
tvqhwp
qoztvz
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int N;
string s, t;
int ans[maxn]; /*string add(string a, string b) {
int len = a.length();
int upup = 0;
string sum = "";
for(int i = len - 1; i >= 0; i --) {
int na = a[i] - 'a';
int nb = b[i] - 'a';
int num = (na + nb) + upup;
if(num > 25) {
upup = 1;
num -= 26;
} else upup = 0;
sum += num + 'a';
}
if(upup) sum = "a" + sum;
return sum;
}*/ int main() {
scanf("%d", &N);
cin >> s >> t;
for(int i = N - ; i >= ; i --) {
int numa = s[i] - 'a';
int numb = t[i] - 'a';
ans[i] = numa + numb;
if(ans[i] % ) {
ans[i + ] += ;
ans[i] = (ans[i] - ) / ;
ans[i] += ans[i + ] / ;
ans[i + ] %= ;
}
else ans[i] = ans[i] / ;
} for(int i = ; i < N; i ++)
printf("%c", 'a' + ans[i]);
printf("\n"); return ;
}
模拟 26 进制加法
F. Graph Without Long Directed Paths
You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.
You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).
The first line contains two integer numbers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤2⋅105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.
The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).
If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line.
Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length mm. The ii-th element of this string should be '0' if the ii-th edge of the graph should be directed from uiui to vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.
6 5
1 5
2 1
1 4
3 1
6 1
YES
10100
The picture corresponding to the first example:
And one of possible answers:
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int N, M;
int col[maxn], st[maxn], en[maxn];
vector<int> v[maxn];
bool flag = true; void dfs(int st, int fa, int color) {
col[st] = color;
if(v[st].size() == ) return;
for(int i = ; i < v[st].size(); i ++) {
if(v[st][i] == fa) continue;
if(col[v[st][i]] == -)
dfs(v[st][i], st, - color);
else if(col[v[st][i]] == col[st])
flag = false;
}
} int main() {
scanf("%d%d", &N, &M);
memset(col, -, sizeof(col));
for(int i = ; i < M; i ++) {
scanf("%d%d", &st[i], &en[i]);
v[st[i]].push_back(en[i]);
v[en[i]].push_back(st[i]);
}
flag = true;
dfs(, -, );
if(!flag) printf("NO\n");
else {
printf("YES\n");
for(int i = ; i < M; i ++) {
if(col[st[i]])
printf("");
else printf("");
}
} return ;
}
CodeForces Round #550 Div.3的更多相关文章
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths
F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...
- D. Equalize Them All Codeforces Round #550 (Div. 3)
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)
F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...
- Codeforces Round #550 (Div. 3) E. Median String (模拟)
Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- (原创)Codeforces Round #550 (Div. 3) A Diverse Strings
A. Diverse Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #550 (Div. 3)E. Median String
把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...
随机推荐
- 【PAT】B1010 一元多项式求导
这道题的用例中没有负数 在输入时就进行结果的计算,忽略常数项, 顺序输出 #include<cstdio> #include<vector> using namespace s ...
- 【2018.05.05 C与C++基础】C++中的自动废料收集:概念与问题引入
在阅读C++语言的设计与演化一书时,作者多次提到希望能设计出一个自动废料收集,然而出于种种考虑,始终未将自动废料收集纳入标准讨论中,而是由Coder自己考虑是否在程序中实现废料收集. 当然了,许多Ja ...
- Python 列表&元组&字典&集合
列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片.合并.删除等操作 可通过索引来向指定位置插入元素 可通过po ...
- X的平方根的golang实现
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 输入: 输出: 输入: 输出: 说明: 的 ...
- 【字符串】ZSC-勤奋的计算机系学生
Description 计算机系的同学从大一就开始学习程序设计语言了.初学者总是容易写出括号不匹配的程序.至今你仍然清楚地记得,那天上机的时候你的程序编译出错,虽然你使尽了吃奶的力气也没有把错误逮着. ...
- 【NOI2008】志愿者招募
[NOI2008]志愿者招募 和[2017山东day7]养猫做法类似. 都是神仙题. 首先我设\(c_{i,j}=[l[j]\leq i\leq r[j]]\) ,于是就可以列出下面的不等式: \[ ...
- php面试题整理(四)
应该是group by username }
- sqrt函数
import numpy as np B = np.arange(3) print (B) print (np.sqrt(B)) #求平方根
- 【转】CefSharp语言(Locales)本地化问题
CefSharp默认是嵌入的chrome浏览器内核,默认英文,所以右键菜单以及一些功能都是英文显示. 国内用需要汉化.CefSharp本身支持本地化Locales,需要在初始化Browser的时候设置 ...
- bak
一.基础篇JVMJVM内存结构堆.栈.方法区.直接内存.堆和栈区别Java内存模型内存可见性.重排序.顺序一致性.volatile.锁.final垃圾回收内存分配策略.垃圾收集器(G1).GC算法.G ...