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)的更多相关文章

  1. 【cf比赛记录】Codeforces Round #601 (Div. 2)

    Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...

  2. 【cf比赛记录】Codeforces Round #600 (Div. 2)

    Codeforces Round #600 (Div. 2) ---- 比赛传送门 昨晚成绩还好,AC A,B题,还能上分(到底有多菜) 补了C.D题,因为昨晚对C.D题已经有想法了,所以补起题来也快 ...

  3. 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...

  4. [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 ...

  5. [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 ...

  6. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  7. 【cf比赛记录】Codeforces Round #604 (Div. 2)

    比赛传送门 感觉这场是最近以来做过的最顺手的一场,持续上分,开心w A了 前三题,然后第四题其实还有半个多小时,但怕身体撑不住,就先退了,其实第四题也很简单 自己认为的算法标签: ​ A.暴力模拟.字 ...

  8. Codeforces Round #617 (Div. 3) String Coloring(E1.E2)

    (easy version): 题目链接:http://codeforces.com/contest/1296/problem/E1 题目一句话就是说,两种颜色不同的字符可以相互换位, 问,对这字符串 ...

  9. Codeforces Round #617 (Div. 3) 补题记录

    1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...

随机推荐

  1. arduino中的serial .available()和serial.read()

    Serial.available() 的意思是:返回串口缓冲区中当前剩余的字符个数.一般用这个函数来判断串口的缓冲区有无数据,当Serial.available()>0时,说明串口接收到了数据, ...

  2. python接口自动化26-发xml格式post请求《转载》

    python接口自动化26-发xml格式post请求 https://cloud.tencent.com/developer/article/1164987

  3. nginx proxy_pass解释

    在当前大部分对外提供的web服务会使用nginx做负载均衡,日常相关的proxy_pass设置有: 以http://192.168.1.101/proxy/test.html进行访问为例子 第一种: ...

  4. 112-PHP类变量之间的赋值标识为同一个对象(二)

    <?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...

  5. 浅谈Python之sys.argv

    (1)sys.argv是什么 sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径.sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序, ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-bold

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. 【剑指Offer】面试题10- I. 斐波那契数列

    题目 写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项.斐波那契数列的定义如下: F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2) ...

  8. wireshark封包详细信息详解(10.15 第二十一天)

    wireshark:网络流量抓取分析神器,需要学习一些常用的数据包过滤规则 IP过滤 ip.addr==192.168.1.1 只要包中的IP有192.168.1.1的,就会提取过来 IP源地址:ip ...

  9. mui 横屏 竖屏

    在项目中只有某个页面需要横屏 ,其他的都是竖屏展示的. 假设a页面横屏 ,返回之后竖屏 b页面 a+ 将其设置为横屏显示: b+ 将其设置为竖屏显示 但是进入a页面之后再返回b页面时 b页面也会称为横 ...

  10. 《新标准C++程序设计》3.1.1-3.1.3(C++学习笔记5)

    构造函数 1.构造函数的概念和作用 (1)概念 构造函数就是一类特殊的成员函数,其名字和类一样,不写返回值类型(void也不可以写),可以有参数,可以重载. 如果定义类时没写构造函数,则编译器生成一个 ...