A. Remove Duplicates
Petya has an array aa consisting of nn integers. He wants to remove duplicate (equal) elements.
Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed.
The first line contains a single integer nn (1≤n≤50) — the number of elements in Petya's array.
The following line contains a sequencea1,a2,…,an (1≤ai≤10001≤ai≤1000) — the Petya's array.
In the first line print integer xx — the number of elements which will be left in Petya's array after he removed the duplicates.
In the second line print xx integers separated with a space — Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left.
6 1 5 5 1 6 1
3 5 6 1
5 2 4 2 4 4
2 2 4
5 6 6 6 6 6
1 6
In the first example you should remove two integers 11, which are in the positions 11 and 44. Also you should remove the integer 55, which is in the position 22.
In the second example you should remove integer 22, which is in the position 11, and two integers 44, which are in the positions 22 and 44.
In the third example you should remove four integers 66, which are in the positions 11, 22, 33 and 44.
去掉重复的,留下重复中最后一个数。
1 #include2 #define ll long long 3 using namespace std; 4 int a[110]; 5 map mp; 6 vector vs; 7 int main() { 8 int n, x; 9 cin >> n;10 for(int i = 0; i < n; i ++) {11 cin >> a[i];12 }13 for(int i = n-1; i >= 0; i --) {14 if(!mp[a[i]]) {15 vs.push_back(a[i]);16 mp[a[i]] = 1;17 }18 }19 cout << vs.size() << endl;20 int len = vs.size();21 for(int i = len-1; i >= 0; i --) printf("%d ",vs[i]);22 printf("\n");23 return 0;24 }
B. File Name
You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.
Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx".
You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 11. For example, if you delete the character in the position 22 from the string "exxxii", then the resulting string is "exxii".
The first line contains integer nn (3≤n≤100) — the length of the file name.
The second line contains a string of length nn consisting of lowercase Latin letters only — the file name.
Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0.
6 xxxiii
1
5 xxoxx
0
10 xxxxxxxxxx
8
In the first example Polycarp tried to send a file with name contains number 3333, written in Roman numerals. But he can not just send the file, because it name contains three letters "x" in a row. To send the file he needs to remove any one of this letters.
有三个或以上的‘x’字符,就删除某些字符使的最多只有两个‘x’字符。问最少删除多少个。
1 #include2 #define ll long long 3 using namespace std; 4 5 int main() { 6 int n; 7 string s; 8 cin >> n >> s; 9 int ans = 0;10 for(int i = 0; i < n; i ++) {11 int cnt = 0;12 while(s[i] =='x') {13 cnt++;14 i++;15 }16 if(cnt >= 3)ans += cnt-2;17 }18 cout << ans << endl;19 return 0;20 }
C. Letters
There are nn dormitories in Berland State University, they are numbered with integers from 11 to nn. Each dormitory consists of rooms, there are aiai rooms in ii-th dormitory. The rooms in ii-th dormitory are numbered from 11 to aiai.
A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all nn dormitories is written on an envelope. In this case, assume that all the rooms are numbered from 11 to a1+a2+⋯+ana1+a2+⋯+an and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.
For example, in case n=2n=2, a1=3a1=3 and a2=5a2=5 an envelope can have any integer from 11 to 88 written on it. If the number 77 is written on an envelope, it means that the letter should be delivered to the room number 44 of the second dormitory.
For each of mm letters by the room number among all nn dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
The first line contains two integers nn and mm (1≤n,m≤2⋅105) — the number of dormitories and the number of letters.
The second line contains a sequence a1,a2,…,an(1≤ai≤1010), where aiai equals to the number of rooms in the ii-th dormitory. The third line contains a sequence b1,b2,…,bm (1≤bj≤a1+a2+⋯+an)(1≤bj≤a1+a2+⋯+an), where bjbj equals to the room number (among all rooms of all dormitories) for the jj-th letter. All bjbj are given in increasing order.
Print mm lines. For each letter print two integers f and k — the dormitory number f(1≤f≤n) and the room number kk in this dormitory (1≤k≤af)(1≤k≤af) to deliver the letter.
3 6 10 15 12 1 9 12 23 26 37
1 1 1 9 2 2 2 13 3 1 3 12
2 3 5 10000000000 5 6 9999999999
1 5 2 1 2 9999999994
In the first example letters should be delivered in the following order:
- the first letter in room 11 of the first dormitory
- the second letter in room 99 of the first dormitory
- the third letter in room 22 of the second dormitory
- the fourth letter in room 1313 of the second dormitory
- the fifth letter in room 11 of the third dormitory
- the sixth letter in room 1212 of the third dormitory
有3栋楼,每楼有ai个,现在从第一栋开始标记为1,知道a1+a2+...+an,
问给定一个数k,求现在在第ji栋,第几号房。二分查找。
1 #include2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 ll a[N], b[N]; 6 int main() { 7 ll n, m; 8 cin >> n >> m >> a[0]; 9 b[0] = a[0];10 for(int i = 1; i < n; i ++) {11 cin >> a[i];12 b[i] = b[i-1] + a[i];13 }14 while(m--) {15 ll x;16 cin >> x;17 int id = lower_bound(b,b+n,x) -b;18 printf("%d %lld\n",id+1,a[id]-(b[id]-x));19 }20 return 0;21 }
D. Almost Arithmetic Progression
Polycarp likes arithmetic progressions. A sequence [a1,a2,…,an][a1,a2,…,an] is called an arithmetic progression if for each ii (1≤i<n1≤i<n) the value ai+1−aiai+1−ai is the same. For example, the sequences [42][42], [5,5,5][5,5,5], [2,11,20,29][2,11,20,29] and [3,2,1,0][3,2,1,0] are arithmetic progressions, but [1,0,1][1,0,1], [1,3,9][1,3,9] and [2,3,1][2,3,1] are not.
It follows from the definition that any sequence of length one or two is an arithmetic progression.
Polycarp found some sequence of positive integers [b1,b2,…,bn][b1,b2,…,bn]. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by 11, an element can be increased by 11, an element can be left unchanged.
Determine a minimum possible number of elements in bb which can be changed (by exactly one), so that the sequence bb becomes an arithmetic progression, or report that it is impossible.
It is possible that the resulting sequence contains element equals 00.
The first line contains a single integer nn (1≤n≤100000)(1≤n≤100000) — the number of elements in bb.
The second line contains a sequence b1,b2,…,bnb1,b2,…,bn (1≤bi≤109)(1≤bi≤109).
If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).
4 24 21 14 10
3
2 500 500
0
3 14 5 1
-1
5 1 3 6 9 12
1
In the first example Polycarp should increase the first number on 11, decrease the second number on 11, increase the third number on 11, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to [25,20,15,10][25,20,15,10], which is an arithmetic progression.
In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.
In the third example it is impossible to make an arithmetic progression.
In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like [0,3,6,9,12][0,3,6,9,12], which is an arithmetic progression.
每个数可以加1、减1,、不变,问最少改变多少次使得数列变成等差数列,否则是-1。有等差数有9种情况。每一中都试下,取可以构成等差数列的最小值。
1 #include2 #define ll long long 3 using namespace std; 4 const int N = 1e5+10; 5 int a[N], b[N]; 6 int dx[] = { 1, 1, 1, 0, 0, 0, -1, -1, -1}, 7 dy[] = { 1, 0, -1, 1, 0, -1, 1, 0, -1}; 8 int main() { 9 int n, s = 0;10 cin >> n;11 for(int i = 0; i < n; i ++) {12 cin >> a[i];13 }14 if(n <= 2) return 0*printf("0\n");15 int ans = 1e7;16 for(int i = 0; i < 9; i ++) {17 int cnt = 0, flag = 1;18 if(dx[i]) cnt++;19 if(dy[i]) cnt++;20 b[0] = a[0] + dx[i];21 b[1] = a[1] + dy[i];22 int tmp = b[1] - b[0];23 for(int j = 2; j < n; j ++) {24 if(a[j]+1-b[j-1] == tmp) {25 b[j] = a[j] + 1;26 cnt++;27 } else if(a[j] - b[j-1] == tmp) b[j] = a[j];28 else if(a[j]-1-b[j-1] == tmp) {29 b[j] = a[j]-1;30 cnt++;31 } else {32 flag = 0;33 break;34 }35 }36 if(flag) ans = min(ans, cnt);37 }38 printf("%d\n",ans==1e7?-1:ans);39 return 0;40 }
E. Bus Video System
The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.
If xx is the number of passengers in a bus just before the current bus stop and yy is the number of passengers in the bus just after current bus stop, the system records the number y−xy−x. So the system records show how number of passengers changed.
The test run was made for single bus and nn bus stops. Thus, the system recorded the sequence of integers a1,a2,…,ana1,a2,…,an (exactly one number for each bus stop), where aiai is the record for the bus stop ii. The bus stops are numbered from 11 to nn in chronological order.
Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww (that is, at any time in the bus there should be from 00 to ww passengers inclusive).
The first line contains two integers nn and ww (1≤n≤1000,1≤w≤109)(1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.
The second line contains a sequence a1,a2,…,ana1,a2,…,an (−106≤ai≤106)(−106≤ai≤106), where aiai equals to the number, which has been recorded by the video system after the ii-th bus stop.
Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.
3 5 2 1 -3
3
2 4 -1 1
4
4 10 2 4 1 2
2
In the first example initially in the bus could be 00, 11 or 22 passengers.
In the second example initially in the bus could be 11, 22, 33 or 44 passengers.
In the third example initially in the bus could be 00 or 11 passenger.
问第1个车站之前车上有多少人,共有多少种情况,不成立就是0。先假设之前有0个人,然后前缀和,取最大值和最小值,小于0的话明天0不成立,这就要算需要加多少使的都不小于0了。然后w-MAX+1,都小于0特判下。
1 #include2 #define ll long long 3 using namespace std; 4 const int N = 1010; 5 ll n, w, a[N]; 6 ll max(ll x, ll y) { 7 return x > y ? x: y; 8 } 9 int main() {10 cin >> n >> w >> a[0];11 for(int i = 1; i < n; i ++) {12 cin >> a[i];13 a[i] += a[i-1];14 }15 ll MIN = 1e12, MAX = -1e12;16 for(int i = 0; i < n; i ++) {17 MIN = min(MIN, a[i]);18 MAX = max(MAX, a[i]);19 }20 // cout << MIN << ' ' << MAX << endl;21 if(MIN < 0 && MAX < 0) {22 cout << max(0,w + MIN+1) << endl;23 } else if(MAX >= 0 && MIN < 0) {24 MAX -= MIN;25 cout << max(0,w - MAX + 1) << endl;26 } else if(MAX >= 0 && MIN >= 0) {27 cout << max(0, w - MAX + 1) << endl;28 }29 return 0;30 }
F. Mentors
In BerSoft nn programmers work, the programmer ii is characterized by a skill riri.
A programmer aa can be a mentor of a programmer bb if and only if the skill of the programmer aa is strictly greater than the skill of the programmer bb (ra>rb)(ra>rb) and programmers aa and bb are not in a quarrel.
You are given the skills of each programmers and a list of kk pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer ii, find the number of programmers, for which the programmer ii can be a mentor.
The first line contains two integers nn and kk (2≤n≤2⋅105(2≤n≤2⋅105, 0≤k≤min(2⋅105,n⋅(n−1)2))0≤k≤min(2⋅105,n⋅(n−1)2)) — total number of programmers and number of pairs of programmers which are in a quarrel.
The second line contains a sequence of integers r1,r2,…,rnr1,r2,…,rn (1≤ri≤109)(1≤ri≤109), where riri equals to the skill of the ii-th programmer.
Each of the following kk lines contains two distinct integers xx, yy (1≤x,y≤n(1≤x,y≤n, x≠y)x≠y) — pair of programmers in a quarrel. The pairs are unordered, it means that if xx is in a quarrel with yy then yy is in a quarrel with xx. Guaranteed, that for each pair (x,y)(x,y) there are no other pairs (x,y)(x,y) and (y,x)(y,x) in the input.
Print nn integers, the ii-th number should be equal to the number of programmers, for which the ii-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
4 2 10 4 10 15 1 2 4 3
0 0 1 2
10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5
5 4 0 5 3 3 9 0 2 5
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
求小于ai的数有多少,并且减去和i有争吵的。
先排序,二分查找小于ai的数有多少个,然后看与 i 有争吵的数是否小于ai,小于的话减一。
1 #include2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 int a[N], b[N], n, k; 6 vector vs[N]; 7 int main() { 8 cin >> n >> k; 9 for(int i = 0; i < n; i ++) {10 cin >> a[i];11 b[i] = a[i];12 }13 sort(b,b+n);14 for(int i = 1, u, v; i <= k; i ++) {15 cin >> u >> v;16 vs[u-1].push_back(v-1);17 vs[v-1].push_back(u-1);18 }19 for(int i = 0; i < n; i ++) {20 int id = lower_bound(b,b+n,a[i])-b;21 for(int j = 0; j < vs[i].size(); j ++) {22 if(a[vs[i][j]] < a[i]) id--;23 }24 printf("%d ",id);25 }26 printf("\n");27 return 0;28 }
G. Petya's Exams
Petya studies at university. The current academic year finishes with nn special days. Petya needs to pass mm exams in those special days. The special days in this problem are numbered from 11 to nn.
There are three values about each exam:
- sisi — the day, when questions for the ii-th exam will be published,
- didi — the day of the ii-th exam (si<disi<di),
- cici — number of days Petya needs to prepare for the ii-th exam. For the ii-th exam Petya should prepare in days between sisi and di−1di−1, inclusive.
There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the ii-th exam in day jj, then si≤j<disi≤j<di.
It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days.
Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible.
The first line contains two integers nn and mm (2≤n≤100,1≤m≤n)(2≤n≤100,1≤m≤n) — the number of days and the number of exams.
Each of the following mm lines contains three integers sisi, didi, cici (1≤si<di≤n,1≤ci≤n)(1≤si<di≤n,1≤ci≤n) — the day, when questions for the ii-th exam will be given, the day of the ii-th exam, number of days Petya needs to prepare for the ii-th exam.
Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given.
If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print nn integers, where the jj-th number is:
- (m+1)(m+1), if the jj-th day is a day of some exam (recall that in each day no more than one exam is conducted),
- zero, if in the jj-th day Petya will have a rest,
- ii (1≤i≤m1≤i≤m), if Petya will prepare for the ii-th exam in the day jj (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it).
Assume that the exams are numbered in order of appearing in the input, starting from 11.
If there are multiple schedules, print any of them.
5 2 1 3 1 1 5 1
1 2 3 0 3
3 2 1 3 1 1 2 1
-1
10 3 4 7 2 1 10 3 8 9 1
2 2 2 1 1 0 4 3 4 4
In the first example Petya can, for example, prepare for exam 11 in the first day, prepare for exam 22 in the second day, pass exam 11 in the third day, relax in the fourth day, and pass exam 22 in the fifth day. So, he can prepare and pass all exams.
In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams.
贪心问题。先让di考试这天全标记为m+1,然后按考试时间排序。对于每一门考试,从[si,di-1]遍历ci次。
1 #include2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 struct Nod{ 6 int a, b, c, id; 7 }nod[N]; 8 bool cmp(Nod &a, Nod &b) { 9 return a.b < b.b;10 }11 int b[N];12 int main() {13 int n, m;14 cin >> n >> m;15 for(int i = 0; i < m; i ++) {16 cin >> nod[i].a >> nod[i].b >> nod[i].c;17 if(b[nod[i].b] != 0) return 0*printf("-1\n");18 b[nod[i].b] = m+1;19 nod[i].id = i+1;20 }21 for(int i = 0; i < m; i ++) {22 int ans = 0;23 for(int j = nod[i].a; j < nod[i].b; j++) {24 if(b[j] == 0) {25 b[j] = nod[i].id;26 ans++;27 }28 if(ans == nod[i].c) break;29 }30 if(ans < nod[i].c) return 0*printf("-1\n");31 }32 for(int i = 1; i <= n; i ++) printf("%d ",b[i]);33 printf("\n");34 return 0;35 }