暑期ACM集训
2019-07-17
08:42:11
这是总结昨天的做题情况
总体来说,好久的没做题了,实力下降了许多,这一个月假又学习吧!!!!
A - Ropewalkers
Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.
The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions aa, bb and ccrespectively. At the end of the performance, the distance between each pair of ropewalkers was at least dd.
Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by 11 (i. e. shift by 11 to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".
You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to dd.
Ropewalkers can walk to negative coordinates, due to the rope is infinite to both
Input
The only line of the input contains four integers aa, bb, cc, dd (1≤a,b,c,d≤1091≤a,b,c,d≤109). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.
Output
Output one integer — the minimum duration (in seconds) of the performance.
Examples
5 2 6 3
2
3 1 5 6
8
8 3 3 2
2
2 3 10 4
3
Note
In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position 88), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be |5−2|=3|5−2|=3, the distance between Boniface and Konrad will be |2−8|=6|2−8|=6 and the distance between Agafon and Konrad will be |5−8|=3|5−8|=3. Therefore, all three pairwise distances will be at least d=3d=3, so the performance could be finished within 2 seconds.
题解:这道题比较简单,签到的
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); long long s[3] = {a, b, c}; sort(s, s + 3); long long int s1 = s[1] - s[0]; long long int s2 = s[2] - s[1]; int j = 2; if (s1 > d) { s1 = 0; j--; } if (s2 > d) { s2 = 0; j--; } cout << d * j - s1 - s2; return 0; }
B - Email from Polycarp
Methodius received an email from his friend Polycarp. However, Polycarp's keyboard is broken, so pressing a key on it once may cause the corresponding symbol to appear more than once (if you press a key on a regular keyboard, it prints exactly one symbol).
For example, as a result of typing the word "hello", the following words could be printed: "hello", "hhhhello", "hheeeellllooo", but the following could not be printed: "hell", "helo", "hhllllooo".
Note, that when you press a key, the corresponding symbol must appear (possibly, more than once). The keyboard is broken in a random manner, it means that pressing the same key you can get the different number of letters in the result.
For each word in the letter, Methodius has guessed what word Polycarp actually wanted to write, but he is not sure about it, so he asks you to help him.
You are given a list of pairs of words. For each pair, determine if the second word could be printed by typing the first one on Polycarp's keyboard.
Input
The first line of the input contains one integer nn (1≤n≤1051≤n≤105) — the number of pairs to check. Further input contains nn descriptions of pairs.
The first line of each description contains a single non-empty word ss consisting of lowercase Latin letters. The second line of the description contains a single non-empty word tt consisting of lowercase Latin letters. The lengths of both strings are not greater than 106106.
It is guaranteed that the total length of all words ss in the input is not greater than 106106. Also, it is guaranteed that the total length of all words tt in the input is not greater than 106106.
Output
Output nn lines. In the ii-th line for the ii-th pair of words ss and tt print YES if the word tt could be printed by typing the word ss. Otherwise, print NO.
Examples
4 hello hello hello helloo hello hlllloo hello helo
YES YES NO NO
5 aa bb codeforces codeforce polycarp poolycarpp aaaa aaaab abcdefghijklmnopqrstuvwxyz zabcdefghijklmnopqrstuvwxyz
NO
NO
YES
NO
NO
题解:我这道题开始想的时用#include <unordered_set>这个做,但是编译器不许通过😭
我有换了另一种方法
这个问题的难点就是hello中出现了两个ll
解决方法就是让两个hello helllloo同步
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; scanf("%lld", &n); while (n--) { string a; string b; cin >> a >> b; ll lenth1 = a.length(); ll lenth2 = b.length(); if (lenth1 > lenth2) { cout << "NO" << endl; continue; } if (a[0] != b[0]) { cout << "NO" << endl; continue; } int i, j; int flag = 0; for (i = 0, j = 0; i < lenth1; i++) { if (a[i] == a[i + 1] && a[i] == b[j]) { j++; continue; } if (a[i] != b[j]) { flag = 1; break; } else { while (a[i] == b[j]) { j++; } } } if (i != lenth1 || j != lenth2 || flag) { cout << "NO" << endl; } else { cout << "YES" << endl; } } return 0; }
C - Exam in BerSU (easy version)
The only difference between easy and hard versions is constraints.
A session has begun at Beland State University. Many students are taking exams.
Polygraph Poligrafovich is going to examine a group of nn students. Students will take the exam one-by-one in order from 11-th to nn-th. Rules of the exam are following:
- The ii-th student randomly chooses a ticket.
- if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam.
- if the student finds the ticket easy, he spends exactly titi minutes to pass the exam. After it, he immediately gets a mark and goes home.
Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.
The duration of the whole exam for all students is MM minutes (maxti≤Mmaxti≤M), so students at the end of the list have a greater possibility to run out of time to pass the exam.
For each student ii, you should count the minimum possible number of students who need to fail the exam so the ii-th student has enough time to pass the exam.
For each student ii, find the answer independently. That is, if when finding the answer for the student i1i1 some student jj should leave, then while finding the answer for i2i2 (i2>i1i2>i1) the student jj student does not have to go home.
Input
The first line of the input contains two integers nn and MM (1≤n≤1001≤n≤100, 1≤M≤1001≤M≤100) — the number of students and the total duration of the exam in minutes, respectively.
The second line of the input contains nn integers titi (1≤ti≤1001≤ti≤100) — time in minutes that ii-th student spends to answer to a ticket.
It's guaranteed that all values of titi are not greater than MM.
Output
Print nn numbers: the ii-th number must be equal to the minimum number of students who have to leave the exam in order to ii-th student has enough time to pass the exam.
Examples
7 15 1 2 3 4 5 6 7
0 0 0 0 0 2 3
5 100 80 40 40 40 60
0 1 1 2 3
Note
The explanation for the example 1.
Please note that the sum of the first five exam times does not exceed M=15M=15 (the sum is 1+2+3+4+5=151+2+3+4+5=15). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are 00.
In order for the 66-th student to pass the exam, it is necessary that at least 22students must fail it before (for example, the 33-rd and 44-th, then the 66-th will finish its exam in 1+2+5+6=141+2+5+6=14 minutes, which does not exceed MM).
In order for the 77-th student to pass the exam, it is necessary that at least 33students must fail it before (for example, the 22-nd, 55-th and 66-th, then the 77-th will finish its exam in 1+3+4+7=151+3+4+7=15 minutes, which does not exceed MM).
题解:比较简单
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int m; cin >>m; int a[150]={0}; for (int i = 0; i < n; i++) { cin >> a[i]; } int b[150]={0}; b[0] = 0; for (int i = 1; i < n; i++) { int q = m; q -= a[i]; int c[150] ={0}; for (int z = 0; z < i; z++) { c[z] = a[z]; } sort(c, c + i); for (int j = 0; j < i; j++) { if (q - c[j] >= 0) { b[i]++; q -= c[j]; } else { break; } } } for (int i = 0; i < n; i++) { if (i != n - 1) cout << i - b[i] << " "; else { cout << i - b[i]; } } return 0; }
D - Exam in BerSU (hard version)
The only difference between easy and hard versions is constraints.
If you write a solution in Python, then prefer to send it in PyPy to speed up execution time.
A session has begun at Beland State University. Many students are taking exams.
Polygraph Poligrafovich is going to examine a group of nn students. Students will take the exam one-by-one in order from 11-th to nn-th. Rules of the exam are following:
- The ii-th student randomly chooses a ticket.
- if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam.
- if the student finds the ticket easy, he spends exactly titi minutes to pass the exam. After it, he immediately gets a mark and goes home.
Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.
The duration of the whole exam for all students is MM minutes (maxti≤Mmaxti≤M), so students at the end of the list have a greater possibility to run out of time to pass the exam.
For each student ii, you should count the minimum possible number of students who need to fail the exam so the ii-th student has enough time to pass the exam.
For each student ii, find the answer independently. That is, if when finding the answer for the student i1i1 some student jj should leave, then while finding the answer for i2i2 (i2>i1i2>i1) the student jj student does not have to go home.
Input
The first line of the input contains two integers nn and MM (1≤n≤2⋅1051≤n≤2⋅105, 1≤M≤2⋅1071≤M≤2⋅107) — the number of students and the total duration of the exam in minutes, respectively.
The second line of the input contains nn integers titi (1≤ti≤1001≤ti≤100) — time in minutes that ii-th student spends to answer to a ticket.
It's guaranteed that all values of titi are not greater than MM.
Output
Print nn numbers: the ii-th number must be equal to the minimum number of students who have to leave the exam in order to ii-th student has enough time to pass the exam.
Examples
7 15 1 2 3 4 5 6 7
0 0 0 0 0 2 3
5 100 80 40 40 40 60
0 1 1 2 3
Note
The explanation for the example 1.
Please note that the sum of the first five exam times does not exceed M=15M=15 (the sum is 1+2+3+4+5=151+2+3+4+5=15). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are 00.
In order for the 66-th student to pass the exam, it is necessary that at least 22students must fail it before (for example, the 33-rd and 44-th, then the 66-th will finish its exam in 1+2+5+6=141+2+5+6=14 minutes, which does not exceed MM).
In order for the 77-th student to pass the exam, it is necessary that at least 33students must fail it before (for example, the 22-nd, 55-th and 66-th, then the 77-th will finish its exam in 1+3+4+7=151+3+4+7=15 minutes, which does not exceed MM).
题解:这道题和上一题唯一的区别在一数据范围开大了很多
这道题由于数据大了很多,所以直接用排序确实要超时,很好的解决方案就是
1~100之内统计,但应当40 40 40 40排除这种情况,适当的减去一些40的个数
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[200000]; ll b[200000]; ll c[200000]; int main() { ll n; cin >> n; ll m; cin >>m; for (ll i = 0; i < n; i++) { cin >> a[i]; } ll sum = 0; ll count[200]={0}; for(int i = 0; i < n; i++) { sum += a[i]; if(sum > m) { ll ant = 0; ll summ = 0; for(int j = 100; j >= 1; j--) { summ += j * count[j]; ant += count[j]; if(sum - summ <= m ) { int x = sum - m; int y = summ - x; ant -= (y/j); cout << ant << " "; break; } } count[a[i]]++; } else { cout << 0 << " "; count[a[i]]++; } } return 0; }
E - Vus the Cossack and a Contest
Vus the Cossack holds a programming competition, in which n people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly m pens and k notebooks.
Determine whether the Cossack can reward all participants, giving each of them at least one pen and at least one notebook.
题解:简单
#include <bits/stdc++.h> using namespace std; int main() { int n,m,k; cin >> n >> m >> k; if(n > m || n > k) { cout << "NO"; } else { cout << "YES"; } return 0; }