Codeforces Round #501 (Div. 3)(ABCDE)

本秂侑毒 提交于 2020-02-10 21:02:07
A. Points in Segments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a set of nn segments on the axis OxOx, each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers lili and riri (1lirim1≤li≤ri≤m) — coordinates of the left and of the right endpoints.

Consider all integer points between 11 and mm inclusive. Your task is to print all such points that don't belong to any segment. The point xxbelongs to the segment [l;r][l;r] if and only if lxrl≤x≤r.

Input

The first line of the input contains two integers nn and mm (1n,m1001≤n,m≤100) — the number of segments and the upper bound for coordinates.

The next nn lines contain two integers each lili and riri (1lirim1≤li≤ri≤m) — the endpoints of the ii-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that li=rili=ri, i.e. a segment can degenerate to a point.

Output

In the first line print one integer kk — the number of points that don't belong to any segment.

In the second line print exactly kk integers in any order — the points that don't belong to any segment. All points you print should be distinct.

If there are no such points at all, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.

Examples
input
Copy
3 52 21 25 5
output
Copy
23 4 
input
Copy
1 71 7
output
Copy
0
Note

In the first example the point 11 belongs to the second segment, the point 22 belongs to the first and the second segments and the point 55belongs to the third segment. The points 33 and 44 do not belong to any segment.

In the second example all the points from 11 to 77 belong to the first segment.

 

区间中有多少个没有被遮住的数

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int a[1000];
 5 int b[1000];
 6 int n,m;
 7 int main(){
 8     cin>>n>>m;
 9     for(int i=0;i<n;i++){
10         int x,y;
11         cin>>x>>y;
12         for(int i=x;i<=y;i++)
13             a[i]=1;
14     }
15     int ans = 0;
16     int cnt = 0;
17     for(int i=1;i<=m;i++){
18         if(a[i]==0){
19             b[ans++] = i;
20             cnt++;
21         }
22     }
23     cout<<cnt<<endl;
24     for(int i=0;i<ans;i++){
25         cout<<b[i]<<" ";
26     }
27     cout<<endl;
28     return 0;
29 }

 

 

B. Obtaining the String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two strings ss and tt. Both strings have length nn and consist of lowercase Latin letters. The characters in the strings are numbered from 11 to nn.

You can successively perform the following move any number of times (possibly, zero):

  • swap any two adjacent (neighboring) characters of ss (i.e. for any i={1,2,,n1}i={1,2,…,n−1} you can swap sisi and si+1)si+1).

You can't apply a move to the string tt. The moves are applied to the string ss one after another.

Your task is to obtain the string tt from the string ss. Find any way to do it with at most 104104 such moves.

You do not have to minimize the number of moves, just find any sequence of moves of length 104104 or less to transform ss into tt.

Input

The first line of the input contains one integer nn (1n501≤n≤50) — the length of strings ss and tt.

The second line of the input contains the string ss consisting of nn lowercase Latin letters.

The third line of the input contains the string tt consisting of nn lowercase Latin letters.

Output

If it is impossible to obtain the string tt using moves, print "-1".

Otherwise in the first line print one integer kk — the number of moves to transform ss to tt. Note that kk must be an integer number between 00and 104104 inclusive.

In the second line print kk integers cjcj (1cj<n1≤cj<n), where cjcj means that on the jj-th move you swap characters scjscj and scj+1scj+1.

If you do not need to apply any moves, print a single integer 00 in the first line and either leave the second line empty or do not print it at all.

Examples
input
Copy
6abcdefabdfec
output
Copy
43 5 4 5 
input
Copy
4abcdaccd
output
Copy
-1
Note

In the first example the string ss changes as follows: "abcdef" → "abdcef" → "abdcfe" → "abdfce" → "abdfec".

In the second example there is no way to transform the string ss into the string tt through any allowed moves.

 

暴力交换就行了.不会超时.

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 int a[10000];
 5 int main(){
 6     int n;
 7     string s,ss;
 8     cin>>n;
 9     cin>>s>>ss;
10     if(s==ss){
11         cout<<"0"<<endl;
12         return 0;
13     }
14     string st = s;
15     sort(st.begin(),st.end());
16     string stt = ss;
17     sort(stt.begin(),stt.end());
18     if(st != stt){
19         cout<<"-1"<<endl;
20         return 0;
21     }
22     int ans = 0;
23     for(int i=0;i<n;i++){
24         if(s[i]!=ss[i]){
25             for(int j = i;j<n;j++){
26                 if(s[j] == ss[i]){
27                     a[ans++] = j;
28                     char c = s[j];
29                     s[j] = s[j-1];
30                     s[j-1] = c;
31                     i--;
32                     break;
33                 }
34             }
35         }
36     }
37     cout<<ans<<endl;
38     for(int i=0;i<ans;i++)
39         cout<<a[i]<<" ";
40     cout<<endl;
41 
42     return 0;
43 }

 

 

C. Songs Compression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ivan has nn songs on his phone. The size of the ii-th song is aiai bytes. Ivan also has a flash drive which can hold at most mm bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all nn songs to the flash drive. He can compress the songs. If he compresses the ii-th song, the size of the ii-th song reduces from aiai to bibi bytes (bi<aibi<ai).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most mm. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to mm).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.

Input

The first line of the input contains two integers nn and mm (1n105,1m1091≤n≤105,1≤m≤109) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next nn lines contain two integers each: the ii-th line contains two integers aiai and bibi (1ai,bi1091≤ai,bi≤109, ai>biai>bi) — the initial size of the ii-th song and the size of the ii-th song after compression.

Output

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.

Examples
input
Copy
4 2110 87 43 15 4
output
Copy
2
input
Copy
4 1610 87 43 15 4
output
Copy
-1
Note

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will

be equal to 8+7+1+5=21218+7+1+5=21≤21. Also Ivan can compress the first and the second songs, then the sum of sizes will be

equal 8+4+3+5=20218+4+3+5=20≤21. Note that compressing any single song is not sufficient to copy all the songs on the flash

drive (for example, after compressing the second song the sum of sizes will be equal to 10+4+3+5=22>2110+4+3+5=22>21).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8+4+1+4=17>168+4+1+4=17>16.

 

求最少需要压缩的数量.

这题贪心.不算难,但是哇了一次.

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 using namespace std;
 4 ll a[1000006];
 5 
 6 bool cmp(ll a,ll b){
 7     return a>b;
 8 }
 9 
10 int main(){
11     ll n,m;
12     cin>>n>>m;
13     ll ans = 0;
14     ll an = 0;
15     for(int i=0;i<n;i++){
16         ll x,y;
17         cin>>x>>y;
18         ans+=y;
19         an+=x;
20         a[i] = x-y;
21     }
22 
23     if(ans>m){
24         cout<<"-1"<<endl;
25         return 0;
26     }
27     if(an<=m){
28         cout<<0<<endl;
29         return 0;
30     }
31     if(ans==m){
32         cout<<n<<endl;
33         return 0;
34     }
35     sort(a,a+n,cmp);
36     ll cnt = 0;
37     // cout<<an<<endl;
38     for(int i = 0;an>m&&i<n;i++){
39         an-=a[i];
40         cnt++;
41     }
42     cout<<cnt<<endl;
43     return 0;
44 }

 

 

D. Walking Between Houses
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially you are in the house 11.

You have to perform kk moves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e.,

in each move the new house differs from the current house). If you go from the house xx to the house yy, the total distance you walked increases

by |xy||x−y| units of distance, where |a||a| is the absolute value of aa. It is possible to visit the same house multiple times (but you can't visit the same house in sequence).

Your goal is to walk exactly ss units of distance in total.

If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly kk moves.

Input

The first line of the input contains three integers nn, kk, ss (2n1092≤n≤109, 1k21051≤k≤2⋅105, 1s10181≤s≤1018) — the number of houses, the number of moves

and the total distance you want to walk.

Output

If you cannot perform kk moves with total walking distance equal to ss, print "NO".

Otherwise print "YES" on the first line and then print exactly kk integers hihi (1hin1≤hi≤n) on the second line, where hihi is the house you visit on the ii-th move.

For each jj from 11 to k1k−1 the following condition should be satisfied: hjhj+1hj≠hj+1. Also h11h1≠1 should be satisfied.

Examples
input
Copy
10 2 15
output
Copy
YES10 4 
input
Copy
10 9 45
output
Copy
YES10 1 10 1 2 1 2 1 6 
input
Copy
10 9 81
output
Copy
YES10 1 10 1 10 1 10 1 10 
input
Copy
10 9 82
output
Copy
NO

 仔细一点写就能写出来,注意特殊情况.

 1 #include <iostream>
 2 #define ll long long int
 3 using namespace std;
 4 
 5 ll n,k,s;
 6 int main(){
 7     cin>>n>>k>>s;
 8     if((n-1)*k<s||k>s){
 9         cout<<"NO"<<endl;
10         return 0;
11     }
12     ll cnt = s%k;
13     ll ans = (s/k) + (cnt>0?1:0);
14     cout<<"YES"<<endl;
15     if(cnt==0){
16         ll index = 1;
17         for(ll i=0;i<k;i++){
18             if(i%2==0){
19                 cout<<index+ans<<" ";
20             }else{
21                 cout<<index<<" ";
22             }
23         }
24         cout<<endl;
25         return 0;
26     }
27     ll index = 1;
28     ll pos = 1;
29     for(ll i=0;i<cnt;i++){
30         if(i%2==0){
31             cout<<index+ans<<" ";
32             pos = index+ans;
33         }else{
34             cout<<index<<" ";
35             pos = index;
36         }
37     }
38     ans--;
39     if(pos==index){
40         ll l = 0;
41         for(ll i=cnt;i<k;i++){
42             if(l%2==0){
43                 cout<<pos+ans<<" ";
44             }else{
45                 cout<<pos<<" ";
46             }
47             l++;
48         }
49     }else{
50         ll l = 0;
51         for(ll i=cnt;i<k;i++){
52             if(l%2==0){
53                 cout<<pos-ans<<" ";
54                 pos = pos-ans;
55             }else{
56                 cout<<pos+ans<<" ";
57                 pos = pos+ans;
58             }
59             l++;
60         }
61     }
62     cout<<endl;
63     return 0;
64 }

 

 

 

E1. Stars Drawing (Easy Edition)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 00 are not allowed).

Let's consider empty cells are denoted by '.', then the following figures are stars:

The leftmost figure is a star of size 11, the middle figure is a star of size 22 and the rightmost figure is a star of size 33.

You are given a rectangular grid of size n×mn×m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 11 to nn, columns are numbered from 11 to mm. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed nmn⋅m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most nmn⋅m stars.

Input

The first line of the input contains two integers nn and mm (3n,m1003≤n,m≤100) — the sizes of the given grid.

The next nn lines contains mm characters each, the ii-th line describes the ii-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.

Output

If it is impossible to draw the given grid using stars only, print "-1".

Otherwise in the first line print one integer kk (0knm0≤k≤n⋅m) — the number of stars needed to draw the given grid. The next kk lines should contain three integers each — xjxj, yjyj and sjsj, where xjxj is the row index of the central star character, yjyj is the column index of the central star character and sjsj is the size of the star. Each star should be completely inside the grid.

Examples
input
Copy
6 8....*......**.....*****....**.......*...........
output
Copy
33 4 13 5 23 5 1
input
Copy
5 5.*...****..****..**......
output
Copy
32 2 13 3 13 4 1
input
Copy
5 5.*...***...*....*........
output
Copy
-1
input
Copy
3 3*.*.*.*.*
output
Copy
-1
Note

In the first example the output

23 4 13 5 2

is also correct.

 

数据量小,暴力前缀

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 char c[105][105];
  6 int a[105][105];
  7 bool vis[105][105];
  8 int dis[4][2]={0,1,0,-1,1,0,-1,0};
  9 int main(){
 10     int x,y;
 11     cin>>x>>y;
 12     for(int i=1;i<=x;i++){
 13         for(int j=1;j<=y;j++){
 14             cin>>c[i][j];
 15         }
 16     }
 17     bool flag = true;
 18     for(int i=1;i<=x;i++){
 19         int ans = 0;
 20         for(int j = 1;j<=y;j++){
 21             if(c[i][j]=='*'){
 22                 ans++;
 23                 flag = false;
 24             }else{
 25                 ans=0;
 26             }
 27             a[i][j] = ans;
 28         }
 29     }
 30     if(flag){
 31         cout<<"0"<<endl;
 32         return 0;
 33     }
 34 
 35     for(int i=1;i<=x;i++){
 36         int ans = 0;
 37         for(int j = y;j>=1;j--){
 38             if(c[i][j]=='*'){
 39                 ans++;
 40             }else{
 41                 ans=0;
 42             }
 43             a[i][j] = min(a[i][j],ans);
 44         }
 45     }
 46 
 47     for(int i=1;i<=y;i++){
 48         int ans = 0;
 49         for(int j = x;j>=1;j--){
 50             if(c[j][i]=='*'){
 51                 ans++;
 52             }else{
 53                 ans=0;
 54             }
 55             a[j][i] = min(a[j][i],ans);
 56         }
 57     }
 58     int cnt = 0;
 59     for(int i=1;i<=y;i++){
 60         int ans = 0;
 61         for(int j = 1;j<=x;j++){
 62             if(c[j][i]=='*'){
 63                 ans++;
 64             }else{
 65                 ans=0;
 66             }
 67             a[j][i] = min(a[j][i],ans);
 68             if(a[j][i]>1){
 69                 cnt++;
 70                 vis[j][i] = 1;
 71                 int x = j,y = i;
 72                 for(int k=0;k<4;k++){
 73                     int xx = x,yy = y;
 74                     for(int p = 1;p<a[j][i];p++){
 75                         xx = xx + dis[k][0];
 76                         yy = yy + dis[k][1];
 77                         vis[xx][yy] = 1;
 78                     }
 79                 }
 80             }
 81 
 82 
 83         }
 84     }
 85     if(cnt==0){
 86         cout<<"-1"<<endl;
 87         return 0;
 88     }
 89     bool ttt = true;
 90     for(int i=1;i<=x;i++){
 91         for(int j = 1;j<=y;j++){
 92             if(c[i][j]=='*'&&vis[i][j]==0){
 93                 ttt = false;
 94                 break;
 95             }
 96         }
 97         if(!ttt) break;
 98     }
 99     if(!ttt){
100         cout<<"-1"<<endl;
101         return 0;
102     }
103     cout<<cnt<<endl;
104     for(int i=1;i<=x;i++){
105         for(int j = 1;j<=y;j++){
106             if(a[i][j]>1&&c[i][j]=='*'){
107                 cout<<i<<" "<<j<<" "<<a[i][j]-1<<endl;
108             }
109         }
110     }
111     return 0;
112 }

 

 

E2. Stars Drawing (Hard Edition)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 00 are not allowed).

Let's consider empty cells are denoted by '.', then the following figures are stars:

The leftmost figure is a star of size 11, the middle figure is a star of size 22 and the rightmost figure is a star of size 33.

You are given a rectangular grid of size n×mn×m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 11 to nn, columns are numbered from 11 to mm. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed nmn⋅m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most nmn⋅m stars.

Input

The first line of the input contains two integers nn and mm (3n,m10003≤n,m≤1000) — the sizes of the given grid.

The next nn lines contains mm characters each, the ii-th line describes the ii-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.

Output

If it is impossible to draw the given grid using stars only, print "-1".

Otherwise in the first line print one integer kk (0knm0≤k≤n⋅m) — the number of stars needed to draw the given grid. The next kk lines should contain three integers each — xjxj, yjyj and sjsj, where xjxj is the row index of the central star character, yjyj is the column index of the central star character and sjsj is the size of the star. Each star should be completely inside the grid.

Examples
input
Copy
6 8....*......**.....*****....**.......*...........
output
Copy
33 4 13 5 23 5 1
input
Copy
5 5.*...****..****..**......
output
Copy
32 2 13 3 13 4 1
input
Copy
5 5.*...***...*....*........
output
Copy
-1
input
Copy
3 3*.*.*.*.*
output
Copy
-1
Note

In the first example the output

23 4 13 5 2

is also correct.

 加大了数据量

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char c[1005][1005];
 4 int vis[1005][1005];
 5 int n,m;
 6 struct Node{
 7     int x,y,index;
 8 }node[1000005];
 9 int pos = 0;
10 int main(){
11     scanf("%d%d",&n,&m);
12     for(int i=0;i<n;i++){
13         scanf("%s",&c[i]);
14     }
15     for(int i=1;i<n-1;i++){
16         for(int j = 1;j<m-1;j++){
17             if(c[i][j] == '*'){
18                 int cnt = 1;
19                 bool flag = true;
20                 while(flag){
21                     int xx = i,yy = j;
22                     if(xx-cnt>=0&&xx+cnt<n&&yy-cnt>=0&&yy+cnt<m){
23                         if(c[xx-cnt][yy]!='*'||c[xx+cnt][yy]!='*'||c[xx][yy-cnt]!='*'||c[xx][yy+cnt]!='*'){
24                             flag = false;
25                             break;
26                         }else{
27                             vis[xx-cnt][yy] =vis[xx+cnt][yy] =vis[xx][yy-cnt] =vis[xx][yy+cnt] = 1;
28                         }
29                     }else{
30                         flag = false;
31                         break;
32                     }
33                         cnt++;
34                 }
35                 if(cnt>1){
36                     node[pos].x = i+1;
37                     node[pos].y = j+1;
38                     node[pos++].index = cnt-1;
39                     vis[i][j] = 1;
40                 }
41             }
42         }
43     }
44     for(int i=0;i<n;i++){
45         for(int j = 0;j<m;j++){
46             if(c[i][j]=='*'&&vis[i][j]==0){
47                 printf("-1\n");
48                 return 0;
49             }
50         }
51     }
52     printf("%d\n",pos);
53     for(int i=0;i<pos;i++){
54         printf("%d %d %d\n",node[i].x,node[i].y,node[i].index);
55     }
56     return 0;
57 }

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!