Codeforces Round #501 (Div. 3) ABDE1E2

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 5
2 2
1 2
5 5
output
Copy
2
3 4
input
Copy
1 7
1 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 <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 int a[N];
 6 int main() {
 7     int n, m, l, r;
 8     cin >> n >> m;
 9     for(int i = 1; i <= n; i ++) {
10         cin >> l >> r;
11         for(int j = l; j <= r; j ++) a[j] = 1;
12     }
13     int ans = 0;
14     for(int i = 1; i <= m; i ++) if(!a[i]) ans++;
15     printf("%d\n",ans);
16     for(int i = 1; i <= m; i ++) if(!a[i]) printf("%d ",i);
17     return 0;
18 }
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
6
abcdef
abdfec
output
Copy
4
3 5 4 5
input
Copy
4
abcd
accd
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.

模拟题,将S变成t

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 110;
 5 char s1[N], s2[N], s3[N];
 6 int a[N], b[N];
 7 int main() {
 8     int n, j;
 9     cin >> n >> s1 >> s2;
10     for(int i = 0; i < n; i ++) a[s1[i]-'a']++, s3[i] = s1[i];
11     for(int i = 0; i < n; i ++) b[s2[i]-'a']++;
12     for(int i = 0; i < 26; i ++) {
13         if(a[i] != b[i]) return 0*printf("-1\n");
14     }
15     int ans = 0;
16     for(int i = 0; i < n; i ++) {
17         if(s1[i] != s2[i]) {
18             for(j = i; j < n; j ++) {
19                 if(s1[j] == s2[i])break;
20             }
21             for(int k = j-1; k >= i; k --) {
22                 ans++;
23                 swap(s1[k],s1[k+1]);
24             }
25         }
26     }
27     printf("%d\n",ans);
28     for(int i = 0; i < n; i ++) {
29         if(s3[i] != s2[i]) {
30             for(j = i; j < n; j ++) {
31                 if(s3[j] == s2[i])break;
32             }
33             for(int k = j-1; k >= i; k --) {
34                 printf("%d ",k+1);
35                 swap(s3[k],s3[k+1]);
36             }
37         }
38     }
39     return 0;
40 }
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 21
10 8
7 4
3 1
5 4
output
Copy
2
input
Copy
4 16
10 8
7 4
3 1
5 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
 3 using namespace std;
 4 const int N = 1e5+10;
 5 struct Nod{
 6     ll a, b;
 7 }e[N];
 8 int a[N], b[N];
 9 bool cmp(const Nod &a, const Nod &b) {
10     return (a.a-a.b) > (b.a-b.b);
11 }
12 int main() {
13     ll n, m, sum = 0, ans = 0;
14     cin >> n >> m;
15     for(int i = 0; i < n; i ++) cin >> e[i].a >> e[i].b, sum += e[i].a;
16     sort(e,e+n,cmp);
17     for(int i = 0; i < n; i ++) {
18         if(sum <= m) break;
19         sum -= (e[i].a-e[i].b);
20         ans++;
21     }
22     if(sum > m) printf("-1\n");
23     else printf("%lld\n",ans);
24     return 0;
25 }
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
YES
10 4
input
Copy
10 9 45
output
Copy
YES
10 1 10 1 2 1 2 1 6
input
Copy
10 9 81
output
Copy
YES
10 1 10 1 10 1 10 1 10
input
Copy
10 9 82
output
Copy
NO


这卡了一个多小时,没有考虑k > s 的情况,不然E1 和E2 也可以过的。
 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 
 6 int main() {
 7     ll n, k, s, t;
 8     cin >> n >> k >> s;
 9     if((n-1)*k < s || k > s) return 0*printf("NO\n");
10     ll now = 1, pre = 1, flag = 1;
11     t = k;
12     printf("YES\n");
13     for(int i = 1; i <= t; i ++) {
14         if(i == t) {
15             if(now+s <= n) return 0*printf("%d\n",now+s);
16             else return 0*printf("%d\n",now-s);
17         }
18         if(s-n+1 >= k-1) {
19             if(flag==1)now += n-1, flag = 2;
20             else now -= n-1, flag = 1;
21             s -= n-1;
22         } else {
23             if(now == 1) {
24                 now++;
25             } else if(now == 2) now--;
26             else if(now == n) now--;
27             else if(now == n-1) now++;
28             s --;
29             // int cnt = s-k+1;
30             // if(now-cnt >= 1) now -= cnt;
31             // else if(now+cnt <= n) now += cnt;
32             // s -= cnt;
33         }
34         k--;
35         printf("%d ",now);
36         pre = now;
37     }
38     return 0;
39 }
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
3
3 4 1
3 5 2
3 5 1
input
Copy
5 5
.*...
****.
.****
..**.
.....
output
Copy
3
2 2 1
3 3 1
3 4 1
input
Copy
5 5
.*...
***..
.*...
.*...
.....
output
Copy
-1
input
Copy
3 3
*.*
.*.
*.*
output
Copy
-1
Note

In the first example the output

2
3 4 1
3 5 2

is also correct.

E1和E2都是下面这个代码,暴力。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1100;
 5 char s[N][N];
 6 bool vis[N][N];
 7 struct Nod {
 8     int x, y, s;
 9 }e[N*N];
10 std::vector<Nod> vs;
11 int n, m;
12 void solve(int x, int y) {
13     int ans = 0, cnt = 1;
14     while(x-cnt >= 1 && x+cnt <= n && y-cnt >= 1 && y+cnt <= m) {
15         if(s[x-cnt][y] == '*' && s[x+cnt][y] == '*' && s[x][y+cnt] == '*' && s[x][y-cnt] == '*') {
16             ans++;
17             vis[x-cnt][y]=vis[x+cnt][y]=vis[x][y-cnt]=vis[x][y+cnt]=vis[x][y] = 1;
18         } else break;
19         cnt++;
20     }
21     if(ans == 0) return;
22     struct Nod p;
23     p.x = x, p.y = y, p.s = ans;
24     vs.push_back(p);
25 }
26 int main() {
27     cin >> n >> m;
28     for(int i = 1; i <= n; i ++) cin >> s[i]+1;
29     for(int i = 2; i <= n-1; i ++) {
30         for(int j = 2; j <= m-1; j ++) {
31             if(s[i][j] == '*') solve(i,j);
32         }
33     }
34     for(int i = 1; i <= n; i ++) {
35         for(int j = 1; j <= m; j ++) {
36             if(s[i][j]=='*') {
37                 if(vis[i][j]==0) return 0*printf("-1\n");
38             }
39         }
40     }
41     printf("%d\n",vs.size());
42     for(int i = 0; i < vs.size(); i ++) {
43         printf("%d %d %d\n",vs[i].x,vs[i].y,vs[i].s);
44     }
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/xingkongyihao/p/9419270.html