Codeforces Round #481 (Div. 3)
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≤501≤n≤50) — the number of elements in Petya's array.
The following line contains a sequence a1,a2,…,ana1,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 #include<bits/stdc++.h> 2 using namespace std; 3 int flag[1005]; 4 int a[55]; 5 int b[55]; 6 int top=0; 7 set<int> s; 8 int main(){ 9 int n; 10 scanf("%d",&n); 11 for(int i=0;i<n;i++){ 12 scanf("%d",&a[i]); 13 s.insert(a[i]); 14 } 15 printf("%d\n",s.size()); 16 for(int i=n-1;i>=0;i--){ 17 if(flag[a[i]]==0){ 18 flag[a[i]]=1; 19 b[top++]=a[i]; 20 } 21 } 22 for(int i=top-1;i>=0;i--){ 23 if(i==top-1) 24 printf("%d",b[i]); 25 else 26 printf(" %d",b[i]); 27 } 28 printf("\n"); 29 }
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 $$$1$$$. For example, if you delete the character in the position $$$2$$$ from the string "exxxii", then the resulting string is "exxii".
The first line contains integer $$$n$$$ $$$(3 \le n \le 100)$$$ — 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 $$$33$$$, 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.
思路:直接暴力;水题;
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[105]; 4 int main(){ 5 int n; 6 scanf("%d",&n); 7 scanf("%s",s); 8 int len=strlen(s); 9 int sum=0; 10 for(int i=0;i<=len-3;i++){ 11 int j; 12 for(j=i;j<i+3;j++){ 13 if(s[j]!='x'){ 14 break; 15 } 16 } 17 if(j==i+3) sum++; 18 } 19 printf("%d\n",sum); 20 }
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)(1≤n,m≤2⋅105) — the number of dormitories and the number of letters.
The second line contains a sequence a1,a2,…,ana1,a2,…,an (1≤ai≤1010)(1≤ai≤1010), where aiai equals to the number of rooms in the ii-th dormitory. The third line contains a sequence b1,b2,…,bmb1,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 ff and kk — the dormitory number ff (1≤f≤n)(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
思路:先求前缀和;再暴力找;水题;
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a[200005]; 4 long long b[200005]; 5 int main(){ 6 int n,m; 7 scanf("%d%d",&n,&m); 8 for(int i=0;i<n;i++){ 9 scanf("%lld",&a[i]); 10 if(i!=0) 11 a[i]=a[i-1]+a[i]; 12 } 13 for(int i=0;i<m;i++){ 14 scanf("%lld",&b[i]); 15 } 16 int t=0; 17 for(int i=0;i<m;i++){ 18 if(b[i]<=a[t]){ 19 if(t==0){ 20 printf("%d %lld\n",t+1,b[i]); 21 } 22 else{ 23 printf("%d %lld\n",t+1,b[i]-a[t-1]); 24 } 25 } 26 else{ 27 while(b[i]>a[t]){ 28 t++; 29 } 30 if(t==0){ 31 printf("%d %lld\n",t+1,b[i]); 32 } 33 else{ 34 printf("%d %lld\n",t+1,b[i]-a[t-1]); 35 } 36 } 37 } 38 }
浙公网安备 33010602011771号