[CF百场计划]Codeforces Round #617 (Div. 3)
A. Array with Odd Sum
Description
You are given an array \(a\) consisting of \(n\) integers.
In one move, you can choose two indices \(1 \le i, j \le n\) such that \(i \ne j\) and set \(a_i := a_j\). You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose \(i\) and \(j\) and replace \(a_i\) with \(a_j\)).
Your task is to say if it is possible to obtain an array with an odd (not divisible by \(2\)) sum of elements.
You have to answer \(t\) independent test cases.
Input
The first line of the input contains one integer \(t\) (\(1 \le t \le 2000\)) — the number of test cases.
The next \(2t\) lines describe test cases. The first line of the test case contains one integer \(n\) (\(1 \le n \le 2000\)) — the number of elements in \(a\). The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2000\)), where \(a_i\) is the \(i\)-th element of \(a\).
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\) (\(\sum n \le 2000\)).
Output
For each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise.
Sample Input
5
2
2 3
4
2 2 8 8
3
3 3 3
4
5 5 5 5
4
1 1 1 1
Sample Output
YES
NO
YES
NO
NO
思路
奇偶性质签到
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int _,n;
scanf("%d",&_);
while(_--){
scanf("%d",&n);
int sum=0,odd=0,even=0;
for(int i=1,s;i<=n;++i){
scanf("%d",&s);
sum+=s;
if(s&1) odd++;
else even++;
}
if(sum&1){
puts("YES");
}else{
if(even==n||odd==n){
puts("NO");
}else{
puts("YES");
}
}
}
return 0;
}
B. Food Buying
Description
Mishka wants to buy some food in the nearby shop. Initially, he has \(s\) burles on his card.
Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number \(1 \le x \le s\), buy food that costs exactly \(x\) burles and obtain \(\lfloor\frac{x}{10}\rfloor\) burles as a cashback (in other words, Mishka spends \(x\) burles and obtains \(\lfloor\frac{x}{10}\rfloor\) back). The operation \(\lfloor\frac{a}{b}\rfloor\) means \(a\) divided by \(b\) rounded down.
It is guaranteed that you can always buy some food that costs \(x\) for any possible value of \(x\).
Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.
For example, if Mishka has \(s=19\) burles then the maximum number of burles he can spend is \(21\). Firstly, he can spend \(x=10\) burles, obtain \(1\) burle as a cashback. Now he has \(s=10\) burles, so can spend \(x=10\) burles, obtain \(1\) burle as a cashback and spend it too.
You have to answer \(t\) independent test cases.
Input
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.
The next \(t\) lines describe test cases. Each test case is given on a separate line and consists of one integer \(s\) (\(1 \le s \le 10^9\)) — the number of burles Mishka initially has.
Output
For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.
Sample Input
6
1
10
19
9876
12345
1000000000
Sample Output
1
11
21
10973
13716
1111111111
思路
签到.看看有多少10
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int _,n;
scanf("%d",&_);
while(_--){
scanf("%d",&n);
int ans=0;
while(n){
if(n>=10){
ans+=(n/10)*10;
n-=(n/10)*9;
}else{
ans+=n;
n=0;
}
}
printf("%d\n",ans);
}
return 0;
}
C. Yet Another Walking Robot
Description
There is a robot on a coordinate plane. Initially, the robot is located at the point \((0, 0)\). Its path is described as a string \(s\) of length \(n\) consisting of characters 'L', 'R', 'U', 'D'.
Each of these characters corresponds to some move:
'L' (left): means that the robot moves from the point \((x, y)\) to the point \((x - 1, y)\); 'R' (right): means that the robot moves from the point \((x, y)\) to the point \((x + 1, y)\); 'U' (up): means that the robot moves from the point \((x, y)\) to the point \((x, y + 1)\); 'D' (down): means that the robot moves from the point \((x, y)\) to the point \((x, y - 1)\). The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point \((x_e, y_e)\), then after optimization (i.e. removing some single substring from \(s\)) the robot also ends its path at the point \((x_e, y_e)\).
This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string \(s\)).
Recall that the substring of \(s\) is such string that can be obtained from \(s\) by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".
You have to answer \(t\) independent test cases.
Input
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.
The next \(2t\) lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the robot's path. The second line of the test case contains one string \(s\) consisting of \(n\) characters 'L', 'R', 'U', 'D' — the robot's path.
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
Output
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers \(l\) and \(r\) such that \(1 \le l \le r \le n\) — endpoints of the substring you remove. The value \(r-l+1\) should be minimum possible. If there are several answers, print any of them.
Sample Input
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
Sample Output
1 2
1 4
3 4
-1
思路
记录到达过的坐标.哈希map
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
char s[maxn];
map<pair<int,int> ,int>mp;
int main() {
int _,n;
scanf("%d",&_);
while(_--){
scanf("%d",&n);
scanf("%s",s+1);
int x=0,y=0;
int l=0,r=0,len=-1;
mp[make_pair(0,0)]=1;
for(int i=1;i<=n;++i){
if(s[i]=='L') x--;
if(s[i]=='R') x++;
if(s[i]=='U') y++;
if(s[i]=='D') y--;
if(mp.count(make_pair(x,y))){
int tmp=mp[make_pair(x,y)];
if(len==-1||len>i-tmp+1){
len=i-tmp+1;
l=tmp,r=i;
}
}
mp[make_pair(x,y)]=i+1;
}
mp.clear();
if(len==-1) puts("-1");
else{
printf("%d %d\n",l,r);
}
}
return 0;
}
D. Fight with Monsters
Description
There are \(n\) monsters standing in a row numbered from \(1\) to \(n\). The \(i\)-th monster has \(h_i\) health points (hp). You have your attack power equal to \(a\) hp and your opponent has his attack power equal to \(b\) hp.
You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to \(0\).
The fight with a monster happens in turns.
You hit the monster by \(a\) hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by \(b\) hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most \(k\) times in total (for example, if there are two monsters and \(k=4\), then you can use the technique \(2\) times on the first monster and \(1\) time on the second monster, but not \(2\) times on the first monster and \(3\) times on the second monster).
Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.
Input
The first line of the input contains four integers \(n, a, b\) and \(k\) (\(1 \le n \le 2 \cdot 10^5, 1 \le a, b, k \le 10^9\)) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.
The second line of the input contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 10^9\)), where \(h_i\) is the health points of the \(i\)-th monster.
Output
Print one integer — the maximum number of points you can gain if you use the secret technique optimally.
Sample Input
6 2 3 3
7 10 50 12 1 8
Sample Output
5
Sample Input
1 1 100 99
100
Sample Output
1
Sample Input
7 4 2 1
1 3 5 4 2 7 6
Sample Output
6
思路
有个很明显的贪心,按照每个怪物最后需要使用多少技能从小到大排序.
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
int s[maxn];
int main() {
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
for(int i=1,p;i<=n;++i){
scanf("%d",&p);
p=(p-1)%(a+b)+1;//最后一块有多少
s[i]=p/a;
if(p%a==0) s[i]--;
}
sort(s+1,s+1+n);
int ans=0;
for(int i=1;i<=n;++i){
if(s[i]<=k){
k-=s[i];
ans++;
}
}
printf("%d",ans);
return 0;
}
E. String Coloring
Description
This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string \(s\) consisting of \(n\) lowercase Latin letters.
You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in \(s\)).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
Input
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of \(s\).
The second line of the input contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.
Output
In the first line print one integer \(res\) (\(1 \le res \le n\)) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.
In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array \(c\) of length \(n\), where \(1 \le c_i \le res\) and \(c_i\) means the color of the \(i\)-th character.
Sample Input
9
abacbecfd
Sample Output
2
1 1 2 1 2 1 2 1 2
Sample Input
8
aaabbcbb
Sample Output
2
1 2 1 2 1 2 1 1
Sample Input
7
abcdedc
Sample Output
3
1 1 1 1 1 2 3
Sample Input
5
abcde
Sample Output
1
1 1 1 1 1
思路
简单版本的只有两个颜色,其实就是找刚好两个不下降的子序列.
困难版本要用到狄尔沃斯定理:它断言:对于任意有限偏序集,其最长链中元素的数目必等于其最小反链划分中反链的数目.
这道题转化之后其实就是找不下降的子序列.可以发现颜色相同的一定不能交换,这说明颜色相同的必须是不下降的子序列.所以这题要求找最小的不下降子序列的划分个数,它等于最长上升子序列.
代码就是比较常见的dp
AC代码
E1
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
char s[250],str[250];
int a,b;
int main() {
int n;
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1;i<=n;++i){
if(s[i]-'a'>=a) str[i]=0+'0',a=s[i]-'a';
else if(s[i]-'a'>=b) str[i]=1+'0',b=s[i]-'a';
else{
puts("NO");
return 0;
}
}
puts("YES");
printf("%s\n",str+1);
return 0;
}
E2
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
int n;
char s[maxn];
int ans[maxn],Ans,a[maxn];
int main() {
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;++i){
ans[i]=a[s[i]-'a'+1]+1;
Ans=max(Ans,ans[i]);
a[s[i]-'a']=max(ans[i],a[s[i]-'a']);
for(int j=s[i]-'a'-1;j>=0;--j){
a[j]=max(a[j],a[s[i]-'a']);
}
}
printf("%d\n",Ans);
for(int i=1;i<n;++i) printf("%d ",ans[i]);
printf("%d\n",ans[n]);
return 0;
}
F. Berland Beauty
Description
There are \(n\) railway stations in Berland. They are connected to each other by \(n-1\) railway sections. The railway network is connected, i.e. can be represented as an undirected tree.
You have a map of that network, so for each railway section you know which stations it connects.
Each of the \(n-1\) sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don't know them. All these values are from \(1\) to \(10^6\) inclusive.
You asked \(m\) passengers some questions: the \(j\)-th one told you three values:
his departure station \(a_j\); his arrival station \(b_j\); minimum scenery beauty along the path from \(a_j\) to \(b_j\) (the train is moving along the shortest path from \(a_j\) to \(b_j\)). You are planning to update the map and set some value \(f_i\) on each railway section — the scenery beauty. The passengers' answers should be consistent with these values.
Print any valid set of values \(f_1, f_2, \dots, f_{n-1}\), which the passengers' answer is consistent with or report that it doesn't exist.
Input
The first line contains a single integer \(n\) (\(2 \le n \le 5000\)) — the number of railway stations in Berland.
The next \(n-1\) lines contain descriptions of the railway sections: the \(i\)-th section description is two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n, x_i \ne y_i\)), where \(x_i\) and \(y_i\) are the indices of the stations which are connected by the \(i\)-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway.
The next line contains a single integer \(m\) (\(1 \le m \le 5000\)) — the number of passengers which were asked questions. Then \(m\) lines follow, the \(j\)-th line contains three integers \(a_j\), \(b_j\) and \(g_j\) (\(1 \le a_j, b_j \le n\); \(a_j \ne b_j\); \(1 \le g_j \le 10^6\)) — the departure station, the arrival station and the minimum scenery beauty along his path.
Output
If there is no answer then print a single integer -1.
Otherwise, print \(n-1\) integers \(f_1, f_2, \dots, f_{n-1}\) (\(1 \le f_i \le 10^6\)), where \(f_i\) is some valid scenery beauty along the \(i\)-th railway section.
If there are multiple answers, you can print any of them.
Sample Input
4
1 2
3 2
3 4
2
1 2 5
1 3 3
Sample Output
5 3 5
Sample Input
6
1 2
1 6
3 1
1 5
4 1
4
6 1 3
3 4 1
6 5 2
1 2 5
Sample Output
5 3 1 2 1
Sample Input
6
1 2
1 6
3 1
1 5
4 1
4
6 1 1
3 4 3
6 5 3
1 2 4
Sample Output
-1
思路
有个很明显的贪心.可以发现如果一条边被两个询问同时覆盖,这条边必然会选择大的那个询问.比如说一个说这条路最小是2,一个说是3,那这条边肯定不能选2,那与3矛盾.所以可以直接每条边暴力赋上最大的那个询问,最后再check一下.
如果没有限制,那就设置为1,因为题中说最小是1.
AC代码
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
vector<int>vt[5002];
struct node{
int u,v,w;
}s[5002];
int dep[5002],f[5002],val[5002];
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
f[u]=fa;
for(int v:vt[u]) {
if (v != fa) dfs(v, u);
}
}
void setVal(int u,int v,int w){
for(int x=u,y=v;x!=y;x=f[x]){
if(dep[x]<dep[y]) swap(x,y);
val[max(x,f[x])]=max(val[max(x,f[x])],w);
}
}
bool check(int u,int v,int w){
int cnt=1e8;
for(int x=u,y=v;x!=y;x=f[x]){
if(dep[x]<dep[y]) swap(x,y);
cnt=min(val[max(x,f[x])],cnt);
}
return cnt==w;
}
int u[5002],v[5002];
int main() {
int n,m;
scanf("%d",&n);
for(int i=2;i<=n;++i){
scanf("%d%d",&u[i],&v[i]);
val[i]=1;
vt[u[i]].push_back(v[i]);
vt[v[i]].push_back(u[i]);
}
dfs(1,0);
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].w);
setVal(s[i].u,s[i].v,s[i].w);
}
bool flag=1;
for(int i=1;i<=m;++i){
if(check(s[i].u,s[i].v,s[i].w)==0) {
flag=0;
break;
}
}
if(flag){
for(int i=2;i<=n;++i){
printf("%d ",val[max(u[i],v[i])]);
}
}else puts("-1");
return 0;
}
[CF百场计划]Codeforces Round #617 (Div. 3)的更多相关文章
- 【cf比赛记录】Codeforces Round #601 (Div. 2)
Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...
- 【cf比赛记录】Codeforces Round #600 (Div. 2)
Codeforces Round #600 (Div. 2) ---- 比赛传送门 昨晚成绩还好,AC A,B题,还能上分(到底有多菜) 补了C.D题,因为昨晚对C.D题已经有想法了,所以补起题来也快 ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- 【cf比赛记录】Codeforces Round #604 (Div. 2)
比赛传送门 感觉这场是最近以来做过的最顺手的一场,持续上分,开心w A了 前三题,然后第四题其实还有半个多小时,但怕身体撑不住,就先退了,其实第四题也很简单 自己认为的算法标签: A.暴力模拟.字 ...
- Codeforces Round #617 (Div. 3) String Coloring(E1.E2)
(easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...
- Codeforces Round #617 (Div. 3) 补题记录
1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...
随机推荐
- S7-300数据处理基本知识(结尾以MW8+1 ADD指令实训仿真,并用状态表监控及刷写变量)
数据处理基本知识汇总 STEP7 的数据类型包括什么? 基本数据类型 复杂数据类型 用于FB(功能块)的输入,输出参数类型 用于FC(功能)的输入,输出参数类型 基本数据类型是什么? 先列举12种数据 ...
- HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值. 分析:lazy标记主要体现在update上. 当l <= L && R <= r时,该结点的子结点 ...
- Pycharm连接Mysql失败. [08001] Could not create connection to database server.
使用Pycharm连接MySQL时遇到如下问题,错误代码[08001] 查了很多资料归纳一下可能是如下几个原因 0.mysql.server没开 找到对应系统下的mysql.server 启动/重启命 ...
- 四十七、在SAP中,把功能区块整合成一个函数,通过调用函数的办法使代码简洁明了
一.我们查看上一次的代码,非常之凌乱,大体可以分为以下这几个区块 二.我们把最后的2个部分,用函数的方式来写,写法如下: 三.执行程序,和之前一样 四.输出结果
- HDU 4921 Map DFS+状态压缩+乘法计数
算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...
- ACM-AK吧!少年
题目描述:AK吧!少年 AK is an ACM competition finished all of the problems. AC This problem is one of the ste ...
- Python 内置类型 dict, list,线程安全吗
近段时间发现一个 Python 连接数据库的连接是线程不安全的,结果惹得我哪哪儿都怀疑变量的多线程是否安全的问题,今天终于找到了正确答案,那就是 Python 内置类型 dict,list ,tupl ...
- linux命令之strace简单使用
strace是什么 strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. 使用方式 strace 使用帮 ...
- Tomcat JDK MySql 安装配置
Tomcat 7绿色版指定jdk并注册服务 https://blog.csdn.net/weixin_43976019/article/details/89386171 例如:service.b ...
- 2.2 学习总结 之 servlet 的两次抽取
说在前面 昨天 完成了文件上出的学习和实践 今天 学习servlet的两次抽取,以加快编写工程的速度 一.servlet 抽取的原因: 刚刚学习使用servlet写后台,往往只使用一个servlet来 ...