A. Contest for Robots
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp is preparing the first programming contest for robots. There are n problems in it, and a lot of robots are going to participate in it. Each robot solving the problem i gets pi points, and the score of each robot in the competition is calculated as the sum of pi over all problems i solved by it. For each problem, pi is an integer not less than 1.
Two corporations specializing in problem-solving robot manufacturing, “Robo-Coder Inc.” and “BionicSolver Industries”, are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results — or manipulating them.
For some reason (which absolutely cannot involve bribing), Polycarp wants the “Robo-Coder Inc.” robot to outperform the “BionicSolver Industries” robot in the competition. Polycarp wants to set the values of pi in such a way that the “Robo-Coder Inc.” robot gets strictly more points than the “BionicSolver Industries” robot. However, if the values of pi will be large, it may look very suspicious — so Polycarp wants to minimize the maximum value of pi over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?
Input
The first line contains one integer n (1≤n≤100) — the number of problems.
The second line contains n integers r1, r2, …, rn (0≤ri≤1). ri=1 means that the “Robo-Coder Inc.” robot will solve the i-th problem, ri=0 means that it won’t solve the i-th problem.
The third line contains n integers b1, b2, …, bn (0≤bi≤1). bi=1 means that the “BionicSolver Industries” robot will solve the i-th problem, bi=0 means that it won’t solve the i-th problem.
Output
If “Robo-Coder Inc.” robot cannot outperform the “BionicSolver Industries” robot by any means, print one integer −1.
Otherwise, print the minimum possible value of maxi=1npi, if all values of pi are set in such a way that the “Robo-Coder Inc.” robot gets strictly more points than the “BionicSolver Industries” robot.
Examples
inputCopy
5
1 1 1 0 0
0 1 1 1 1
outputCopy
3
inputCopy
3
0 0 0
0 0 0
outputCopy
-1
inputCopy
4
1 1 1 1
1 1 1 1
outputCopy
-1
inputCopy
9
1 0 0 0 0 0 0 0 1
0 1 1 0 1 1 1 1 0
outputCopy
4
Note
In the first example, one of the valid score assignments is p=[3,1,3,1,1]. Then the “Robo-Coder” gets 7 points, the “BionicSolver” — 6 points.
In the second example, both robots get 0 points, and the score distribution does not matter.
In the third example, both robots solve all problems, so their points are equal.
//Pretests passed 第一遍 通过预测试
// 1A
#include<iostream>
#include<cstring>
#include<algorithm>
#define Maxn 102
using namespace std;
int a[Maxn],b[Maxn];
int main(int argc,char* argv[]) {
int n,A = 0,B = 0,Ans;
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
for(int i=1; i<=n; i++) {
scanf("%d",&b[i]);
if(b[i] == a[i]) continue;
if(a[i] == 1 && b[i] == 0) A++;
if(a[i] == 0 && b[i] == 1) B++;
}
if(A == 0) Ans = -1;
else {
if(A == 1) Ans = B + 1;
else Ans = B / A + 1;
}
printf("%d\n",Ans);
return 0;
}
B. Journey Planning
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c1 to start her journey. She will visit it, and after that go to some other city c2>c1, then to some other city c3>c2, and so on, until she chooses to end her journey in some city ck>ck−1. So, the sequence of visited cities [c1,c2,…,ck] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value bi associated with it. If there is only one city in Tanya’s journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities ci and ci+1, the condition ci+1−ci=bci+1−bci must hold.
For example, if n=8 and b=[3,4,4,6,6,7,8,9], there are several three possible ways to plan a journey:
c=[1,2,4];
c=[3,5,6,8];
c=[7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1≤n≤2⋅105) — the number of cities in Berland.
The second line contains n integers b1, b2, …, bn (1≤bi≤4⋅105), where bi is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
inputCopy
6
10 7 1 9 10 15
outputCopy
26
inputCopy
1
400000
outputCopy
400000
inputCopy
7
8 9 26 11 12 29 14
outputCopy
55
Note
The optimal journey plan in the first example is c=[2,4,5].
The optimal journey plan in the second example is c=[1].
The optimal journey plan in the third example is c=[3,6].
Test: #7, time: 2000 ms., memory: 2344 KB, exit code: -1, checker exit code: 0, verdict: TIME_LIMIT_EXCEEDED
Input
200000
200001 200002 200003 200004 200005 200006 200007 200008 200009 200010 200011 200012 200013 200014 200015 200016 200017 200018 200019 200020 200021 200022 200023 200024 200025 200026 200027 200028 200029 200030 200031 200032 200033 200034 200035 200036 200037 200038 200039 200040 200041 200042 200043 200044 200045 200046 200047 200048 200049 200050 200051 200052 200053 200054 200055 200056 200057 200058 200059 200060 200061 200062 200063 200064 200065 200066 200067 200068 200069 200070 200071 200072…
// 时间 : 2020年3月1日21:51:21
// 625 Div2 B.Journey Planning
// Time limit exceeded on pretest 7
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define Maxn 100005
#define LL long long
using namespace std;
int b[Maxn * 2];
LL f[Maxn * 2];
int main(int argc,char* argv[]) {
int n; scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&b[i]),f[i] = b[i];
for(int i=2; i<=n; i++)
for(int j=1; j<i; j++) {
if(i - j == b[i] - b[j]) f[i] = max(f[i],f[j] + b[i]);
}
LL Ans = 0;
for(int i=1; i<=n; i++) Ans = max(Ans,f[i]);
printf("%lld\n",Ans);
return 0;
}
做完写的时候,感觉正解来了,但是恰好比赛结束了
Test8
200000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 201 203 205 207 209 211 213 215 217 219 221 223 225 227 229 231 233 235 237 239 241 243 245 247 249 251 253 255 257 259 261 263 265 267 269 271 273 275 277 27…
// 时间 : 2020年3月1日21:51:21
// 625 Div2 B.Journey Planning
// 时间复杂度 是问题
// 思路: 用一个线性的数据结构 维护i 前面的最大的f[i] 在哪里 对于每个i
// 从他前面最大的f[i] 里找 一旦最大的 匹配 i-j == f[i] - f[j] break;
// 很遗憾 改出来的时候(现在还不知道A不A 估计 悬)Contest is over
// Time limit exceeded on test 8
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#define Maxn 100005
#define LL long long
using namespace std;
int b[Maxn * 2];
LL f[Maxn * 2];
struct Node {
int pos; LL val;
bool operator < (const Node &a) const {
return val < a.val;
}
};
priority_queue<Node> q;
queue<Node> Q;
int main(int argc,char* argv[]) {
int n; scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&b[i]),f[i] = b[i];
Node p; p.pos = 1; p.val = f[1];
q.push(p);
for(int i=2; i<=n; i++){
while(!q.empty()) {
p = q.top(); q.pop(); Q.push(p);
if(i - p.pos == b[i] - b[p.pos]) {
f[i] = max(f[i],p.val + b[i]);
break;
}
}
p.pos = i; p.val = f[i]; q.push(p);
while(!Q.empty()) {
q.push(Q.front());
Q.pop();
}
}
LL Ans = 0;
for(int i=1; i<=n; i++) Ans = max(Ans,f[i]);
printf("%lld\n",Ans);
return 0;
}
【正解:】 从公式上下手,昨晚做的时候我还想应该就是是从公式上下手,但是无奈好久没写代码,脑子变笨了,愣是没看出来,题目要求条件ci+1-ci==bci+1 - bci 移项一下 bci+1 -ci+1= bci-ci 差相等的就求和
// 时间 : 2020年3月1日21:51:21
// 625 Div2 B.Journey Planning
// 时间复杂度 是问题
// 思路: 用一个线性的数据结构 维护i 前面的最大的f[i] 在哪里 对于每个i
// 从他前面最大的f[i] 里找 一旦最大的 匹配 i-j == f[i] - f[j] break;
// 很遗憾 改出来的时候(现在还不知道A不A 估计 悬)Contest is over
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define Maxn 100005
#define LL long long
using namespace std;
struct Data {
int b,c,sub;
}a[Maxn * 2];
inline bool cmp(Data P,Data Q) {
return P.sub < Q.sub;
}
int main(int argc,char* argv[]) {
int n; scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i].b);
a[i].c = i;
a[i].sub = a[i].b - a[i].c;
}
sort(a + 1,a + 1 + n,cmp);
LL Ans = a[1].b,tmp = a[1].b;
for(int i=2; i<=n; i++) {
if(a[i].sub == a[i - 1].sub) tmp += a[i].b;
else {
Ans = max(Ans,tmp);
tmp = a[i].b;
}
}
Ans = max(Ans,tmp);// 必须有 考虑一下
printf("%lld\n",Ans);
return 0;
}
C. Remove Adjacent
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of lowercase Latin letters. Let the length of s be |s|. You may perform several operations on this string.
In one operation, you can choose some index i and remove the i-th character of s (si) if at least one of its adjacent characters is the previous letter in the Latin alphabet for si. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index i should satisfy the condition 1≤i≤|s| during each operation.
For the character si adjacent characters are si−1 and si+1. The first and the last characters of s both have only one adjacent character (unless |s|=1).
Consider the following example. Let s= bacabcab.
During the first move, you can remove the first character s1= b because s2= a. Then the string becomes s= acabcab.
During the second move, you can remove the fifth character s5= c because s4= b. Then the string becomes s= acabab.
During the third move, you can remove the sixth character s6=‘b’ because s5= a. Then the string becomes s= acaba.
During the fourth move, the only character you can remove is s4= b, because s3= a (or s5= a). The string becomes s= acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
Input
The first line of the input contains one integer |s| (1≤|s|≤100) — the length of s.
The second line of the input contains one string s consisting of |s| lowercase Latin letters.
Output
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
Examples
inputCopy
8
bacabcab
outputCopy
4
inputCopy
4
bcda
outputCopy
3
inputCopy
6
abbbbb
outputCopy
5
Note
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is 4.
In the second example, you can remove all but one character of s. The only possible answer follows.
During the first move, remove the third character s3= d, s becomes bca.
During the second move, remove the second character s2= c, s becomes ba.
And during the third move, remove the first character s1= b, s becomes a.
贪心: 先从z开始删除,然后删除y…
注意仔细读题,刚开始我一度以为样例1 是错的
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string s;
int main(int argc,char* argv[]) {
int n; scanf("%d",&n);
cin >> s;
for(int i='z'; i>='a'; i--)
for(int j=1; j<=100; j++)
for(int k=0; k<s.size(); k++) {
if(s[k] == i){
if(s[k - 1] == i - 1 || s[k + 1] == i - 1)
s.erase(k,1),--k;
}
}
printf("%d\n",n - s.size());
return 0;
}
D. Navigation System
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p1, p2, …, pk, where p1=s, pk=t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp’s path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp’s car has a complex navigation system installed in it. Let’s describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let’s denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1,2,3,4] (s=1, t=4):
Check the picture by the link http://tk.codeforces.com/a.png
When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1,5,4];
Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2,6,4] (note that it could choose [2,3,4]);
Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3,4];
Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2,3,4] instead of [2,6,4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3,4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp’s path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2≤n≤m≤2⋅105) — the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1≤u,v≤n, u≠v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u,v) appears at most once in these m lines (but if there is a road (u,v), the road (v,u) can also appear).
The following line contains one integer k (2≤k≤n) — the number of intersections in Polycarp’s path from home to his workplace.
The last line contains k integers p1, p2, …, pk (1≤pi≤n, all these integers are pairwise distinct) — the intersections along Polycarp’s path in the order he arrived at them. p1 is the intersection where Polycarp lives (s=p1), and pk is the intersection where Polycarp’s workplace is situated (t=pk). It is guaranteed that for every i∈[1,k−1] the road from pi to pi+1 exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
inputCopy
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
outputCopy
1 2
inputCopy
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
outputCopy
0 0
inputCopy
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
outputCopy
0 3
建反向边,每次判断,,,,, 好好读题
原来Div2 并没有想象中的那么难,不过对于比赛来说,两个小时的比赛时间,读题(翻译)真的占去了我不少时间
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#define Maxn 200005
using namespace std;
bool exist[Maxn];
int dist[Maxn],a[Maxn],p;
vector<int> g[Maxn],G[Maxn];
void Dijkstra() {
memset(dist,0x7f,sizeof(dist));
dist[a[p]] = 0;
queue<int> q; q.push(a[p]);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i=0; i<G[u].size(); i++) {
int v = G[u][i];
if(dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
int main(int argc,char* argv[]) {
int n,m,u,v; scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
G[v].push_back(u);
}
scanf("%d",&p);
for(int i=1; i<=p; i++) scanf("%d",&a[i]);
Dijkstra();
int Mininm = 0,Maxinm = 0;
for(int i=1; i<p; i++) {
u = a[i],v = a[i + 1];
if(dist[u] != dist[v] + 1) ++Mininm,++Maxinm;
else {
for(int j=0; j<g[u].size(); j++) {
int x = g[u][j];
if(dist[x] == dist[u] - 1 && x != v) {
++Maxinm;
break;
}
}
}
}
printf("%d %d\n",Mininm,Maxinm);
return 0;
}