Petya has an array n 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 1≤n≤50) — the number of elements in Petya's array.
The following line contains a sequence1≤ai≤1000) — the Petya's array.
In the first line print integer x — the number of elements which will be left in Petya's array after he removed the duplicates.
In the second line print x 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 2.
In the second example you should remove integer 4.
In the third example you should remove four integers 4.
去掉重复的,留下重复中最后一个数。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int a[110]; 5 map<int,int> mp; 6 vector<int> 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
The first line contains integer — the length of the file name.
The second line contains a string of length n 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
有三个或以上的‘x’字符,就删除某些字符使的最多只有两个‘x’字符。问最少删除多少个。
1 #include <bits/stdc++.h> 2 #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 ai.
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 a1+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 4 of the second dormitory.
For each of n dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
The first line contains two integers — the number of dormitories and the number of letters.
The second line contains a sequence increasing order.
Print (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 1 of the first dormitory
- the second letter in room 9 of the first dormitory
- the third letter in room 2 of the second dormitory
- the fourth letter in room 13 of the second dormitory
- the fifth letter in room 1 of the third dormitory
- the sixth letter in room 12 of the third dormitory
有3栋楼,每楼有ai个,现在从第一栋开始标记为1,知道a1+a2+...+an,
问给定一个数k,求现在在第ji栋,第几号房。二分查找。
1 #include <bits/stdc++.h> 2 #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 [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 1, an element can be left unchanged.
Determine a minimum possible number of elements in b becomes an arithmetic progression, or report that it is impossible.
It is possible that the resulting sequence contains element equals 0.
The first line contains a single integer b.
The second line contains a sequence (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 [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], which is an arithmetic progression.
每个数可以加1、减1,、不变,问最少改变多少次使得数列变成等差数列,否则是-1。有等差数有9种情况。每一中都试下,取可以构成等差数列的最小值。
1 #include <bits/stdc++.h> 2 #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 y−x. So the system records show how number of passengers changed.
The test run was made for single bus and n 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 w passengers inclusive).
The first line contains two integers (1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.
The second line contains a sequence i-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 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 2 passengers.
In the second example initially in the bus could be 4 passengers.
In the third example initially in the bus could be 1 passenger.
问第1个车站之前车上有多少人,共有多少种情况,不成立就是0。先假设之前有0个人,然后前缀和,取最大值和最小值,小于0的话明天0不成立,这就要算需要加多少使的都不小于0了。然后w-MAX+1,都小于0特判下。
1 #include <bits/stdc++.h> 2 #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 ri.
A programmer b are not in a quarrel.
You are given the skills of each programmers and a list of i can be a mentor.
The first line contains two integers 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 i-th programmer.
Each of the following (y,x) in the input.
Print i-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 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 2e5+10; 5 int a[N], b[N], n, k; 6 vector<int> 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 n.
There are three values about each exam:
- i-th exam will be published,
- si<di),
- di−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 si≤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 (2≤n≤100,1≤m≤n) — the number of days and the number of exams.
Each of the following i-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 j-th number is:
- j-th day is a day of some exam (recall that in each day no more than one exam is conducted),
- zero, if in the j-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 1.
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 2 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 #include <bits/stdc++.h> 2 #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 }