A. Two distinct pointsc++
You are given two segments [l1;r1] and [l2;r2] on the x-axis. It is guaranteed that l1<r1 and l2<r2. Segments may intersect, overlap or even coincide with each other.app
Your problem is to find two integers a and b such that l1≤a≤r1, l2≤b≤r2 and a≠b. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment [l1;r1] and the second one belongs to the segment [l2;r2].ide
It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.ui
You have to answer q independent queries.this
Input
The first line of the input contains one integer q (1≤q≤500) — the number of queries.spa
Each of the next q lines contains four integers l1i,r1i,l2i and r2i (1≤l1i,r1i,l2i,r2i≤109,l1i<r1i,l2i<r2i) — the ends of the segments in the i-th query.rest
Output
Print 2q integers. For the i-th query print two integers ai and bi — such numbers that l1i≤ai≤r1i, l2i≤bi≤r2i and ai≠bi. Queries are numbered in order of the input.code
It is guaranteed that the answer exists. If there are multiple answers, you can print any.blog
Example
input
5
1 2 1 2
2 6 3 4
2 4 1 3
1 2 1 3
1 4 5 8
output
2 1
3 4
3 2
1 2
3 7排序
題意:給出L1, R1, L2, R2,求兩個數知足L1 <= a <= R1, L2 <= b <= R2, a != b。
思路:先仔細讀題。。開始我連a != b都沒注意到真是@#%¥。爲了節約時間考慮各類特判我直接粗暴的進行了兩重for循環,,反正不會TLE。
代碼:略
B. Divisors of Two Integers
Recently you have received two positive integer numbers x and y. You forgot them, but you remembered a shuffled list containing all divisors of x (including 1 and x) and all divisors of y (including 1 and y). If d is a divisor of both numbers x and y at the same time, there are two occurrences of d in the list.
For example, if x=4 and y=6 then the given list can be any permutation of the list [1,2,4,1,2,3,6]. Some of the possible lists are: [1,1,2,4,6,3,2], [4,6,1,1,2,3,2] or [1,6,3,2,4,1,2].
Your problem is to restore suitable positive integer numbers x and y that would yield the same list of divisors (possibly in different order).
It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers x and y.
Input
The first line contains one integer n (2≤n≤128) — the number of divisors of x and y.
The second line of the input contains n integers d1,d2,…,dn (1≤di≤104), where di is either divisor of x or divisor of y. If a number is divisor of both numbers x and y then there are two copies of this number in the list.
Output
Print two positive integer numbers x and y — such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
Example
input
10
10 2 8 1 2 4 1 20 4 5
output
20 8
題意:給出n個數,他們是兩個數x, y的因子,求出x, y。特別地,若是某個數是x, y的公因子,它會出現兩次。
思路:考慮到n不大,先粗暴地從大到小排序,根據性質易得當前最大數即是x, y其中較大一個;而後依次從數列中刪去該數的因子,剩下的數中那個最大數即x, y其中較小一個。
代碼:略
C. Nice Garland
You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the i-th lamp is si ('R', 'G' and 'B' — colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is t, then for each i,j such that ti=tj should be satisfied |i−j| mod 3=0. The value |x| means absolute value of x, the operation x mod y means remainder of x when divided by y.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of lamps.
The second line of the input contains the string s consisting of n characters 'R', 'G' and 'B' — colors of lamps in the garland.
Output
In the first line of the output print one integer r — the minimum number of recolors needed to obtain a nice garland from the given one.
In the second line of the output print one string t of length n — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
Examples
input
3
BRB
output
1
GRB
input
7
RGBGRBB
output
3
RGBRGBR
題意:包含RGB三個字符的一個數列,要求更換部分字符,使其任意相同字符之間的間距大於2,舉個例子,RBGR,合法;RBR,不合法。
思路:很巧妙的一道題,注意到若是要知足RGB三個字符同時知足題目條件,只有諸如RGBRGBRG...或者GRBGRBGRB...的數列知足。這樣咱們能夠暴力地打出6種有限循環數列的狀況,而後和原數列進行比較,字符不一樣個數最少的數列爲知足題意的答案。
代碼:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define MAXN 200005 5 #define INF 1 << 30 6 7 const int num[6][3] = { 8 {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} 9 }; 10 11 int n, a[MAXN], tot, ans = INF, x; 12 char ch[MAXN]; 13 14 int main() { 15 scanf("%d", &n); 16 scanf("%s", ch + 1); 17 for (int i = 1; i <= n; i++) a[i] = ch[i] == 'R' ? 1 : ch[i] == 'G' ? 2 : 3; 18 for (int i = 0; i <= 5; i++) { 19 tot = 0; 20 for (int j = 1; j <= n; j++) tot += a[j] != num[i][j % 3]; 21 if (tot < ans) ans = tot, x = i; 22 } 23 printf("%d\n", ans); 24 for (int i = 1; i <= n; i++) printf(num[x][i % 3] == 1 ? "R" : num[x][i % 3] == 2 ? "G" : "B"); 25 }
D. Diverse Garland
You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the i-th lamp is si ('R', 'G' and 'B' — colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 1) have distinct colors.
In other words, if the obtained garland is t then for each i from 1 to n−1 the condition ti≠ti+1 should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of lamps.
The second line of the input contains the string s consisting of n characters 'R', 'G' and 'B' — colors of lamps in the garland.
Output
In the first line of the output print one integer r — the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string t of length n — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
Examples
input
9
RBGRRBRGG
output
2
RBGRGBRGR
input
8
BBBGBRRR
output
2
BRBGBRGR
input
13
BBRRRRGGGGGRR
output
6
BGRBRBGBGBGRG
題意:包含RGB三個字符的一個數列,要求更換部分字符,使其任意相同字符不相鄰。
思路:D和C算是姊妹題吧,不過思路徹底不同,我的以爲D題思路更容易得出,主要是樣例給得至關完美了。從樣例中咱們容易得出,咱們能夠對全部連着字符都相同的子串進行拆分,而後對其隔一個字符修改一個,對於連續個數爲奇數的狀況,修改非最左端右端的字符,修改後的字符只需知足非原字符便可,如GGGGG可修改成GBGBG;對於連續個數爲偶數的狀況,若是從最左端開始修改,則需考慮該連續字符子串左端的字符,修改後的字符應與其不一樣,如BGGGG可修改成BRGRG。不過能夠不這樣特殊考慮,詳細見代碼。
代碼:
1 #include <bits/stdc++.h> 2 #define MAXN 200005 3 4 const char col[3] = {'R', 'G', 'B'}; 5 int n, ans, r; 6 char a[MAXN]; 7 8 int main() { 9 scanf("%d", &n); 10 scanf("%s", a + 1); 11 for (int l = 1; l < n;) { 12 if (a[l] == a[l + 1]) { 13 int tot = 0; 14 for (int j = l + 1; j <= n + 1; j++, tot++) 15 if (a[j] != a[l]) { 16 r = j - 1; 17 break; 18 } 19 for (int j = l - tot % 2 + 1; j <= r; j += 2) 20 for (int k = 0; k <= 2; k++) 21 if (col[k] != a[j] && col[k] != a[j - 1]) { 22 a[j] = col[k]; 23 break; 24 } 25 ans += (tot + 1)/ 2; 26 l = r + 1; 27 } 28 else l++; 29 } 30 printf("%d\n", ans); 31 for (int i = 1; i <= n; i++) printf("%c", a[i]); 32 return 0; 33 }
E1. Array and Segments (Easy version)
The only difference between easy and hard versions is a number of elements in the array.
You are given an array a consisting of n integers. The value of the i-th element of the array is ai.
You are also given a set of m segments. The j-th segment is [lj;rj], where 1≤lj≤rj≤n.
You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array a=[0,0,0,0,0] and the given segments are [1;3] and [2;4] then you can choose both of them and the array will become b=[−1,−2,−2,−1,0].
You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array a and obtain the array b then the value maxi=1nbi−mini=1nbi will be maximum possible.
Note that you can choose the empty set.
If there are multiple answers, you can print any.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and m (1≤n≤300,0≤m≤300) — the length of the array a and the number of segments, respectively.
The second line of the input contains n integers a1,a2,…,an (−106≤ai≤106), where ai is the value of the i-th element of the array a.
The next m lines are contain two integers each. The j-th of them contains two integers lj and rj (1≤lj≤rj≤n), where lj and rj are the ends of the j-th segment.
Output
In the first line of the output print one integer d — the maximum possible value maxi=1nbi−mini=1nbi if b is the array obtained by applying some subset of the given segments to the array a.
In the second line of the output print one integer q (0≤q≤m) — the number of segments you apply.
In the third line print q distinct integers c1,c2,…,cq in any order (1≤ck≤m) — indices of segments you apply to the array a in such a way that the value maxi=1nbi−mini=1nbi of the obtained array b is maximum possible.
If there are multiple answers, you can print any.
Examples
input
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
output
6
2
1 4
input
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
output
7
2
3 2
input
1 0
1000000
output
0
0
Note
In the first example the obtained array b will be [0,−4,1,1,2] so the answer is 6.
In the second example the obtained array b will be [2,−3,1,−1,4] so the answer is 7.
In the third example you cannot do anything so the answer is 0.
題意:給出有n個數的數列和m個區間,要求選出部分區間,對於每一區間中的全部數減1,使數列最大值與最小值之差最大。
思路:
一、對於任一區間,若是其中包含了最大值與最小值,或者均不包含,則選擇該區間對結果沒有影響;若是隻包含最大值,則不選;只包含最小值,則選。
二、數列最大值和最小值是隨着咱們的每一次操做而變化的,並非某一個肯定的數。這裏咱們能夠採起一種較爲暴力的方法,即枚舉每個數做爲預期最小值。
三、根據第一條可得,若是區間包含了咱們設定的最小值,則選擇該區間必然會趨於更優解,這樣問題就簡化很多了;
四、綜上,方法是,枚舉預期最小值,對於該值,選取全部包含該值的區間進行修改,最後算得每一種狀況最大最小值之差中最大的狀況便可,最壞複雜度爲O(n ^ 2 * m)。
代碼:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define MAXN 305 5 #define INF 1 << 30 6 7 int n, m, a[MAXN], l[MAXN], r[MAXN], b[MAXN], ans, ai, at; 8 9 int main() { 10 scanf("%d %d", &n, &m); 11 for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 12 for (int i = 1; i <= m; i++) scanf("%d %d", &l[i], &r[i]); 13 for (int i = 1; i <= n; i++) { 14 int tot = 0, mx = -INF, mi = INF; 15 for (int j = 1; j <= n; j++) b[j] = a[j]; 16 for (int j = 1; j <= m; j++) 17 if (l[j] <= i && i <= r[j]) { 18 tot++; 19 for (int k = l[j]; k <= r[j]; k++) b[k]--; 20 } 21 for (int j = 1; j <= n; j++) 22 mx = max(b[j], mx), mi = min(b[j], mi); 23 if (mx - mi > ans) ans = mx - mi, ai = i, at = tot; 24 } 25 printf("%d\n%d\n", ans, at); 26 for (int i = 1; i <= m; i++) 27 if (l[i] <= ai && ai <= r[i]) printf("%d ", i); 28 29 }
E2. Array and Segments (Hard version)
題意:題幹略。困難版與簡單版的惟一區別爲n的取值由1 <= n <= 300上調至1 <= n <= 10 ^ 5。
思路:
n被增強以後,簡單版本的方法顯然不適用了。但因爲題目從頭至尾都是進行區間修改以及區間查詢,能夠瓜熟蒂落地想到線段樹。剛開始作的時候備受困擾——難道每次選擇一個新的數時都要從新初始化一次線段樹嘛??
看了其餘人的思路後才豁然開朗——咱們發現,若是咱們從選擇i爲最小值變成選擇i + 1時,只有兩種區間會出現選擇的變化——[x, i],以及[i + 1, y]。對於前者咱們將[x, i]所有加1,後者咱們將[i + 1, y]所有減1便可。
代碼:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define MAXN 100005 5 #define INF 1 << 30 6 7 int n, m, a[MAXN], l[MAXN], r[MAXN], b[MAXN], tot, ans, ai, ql, qr; 8 9 struct Tree { 10 int mx, mi, f; 11 } t[MAXN << 2], ti[MAXN << 2]; 12 13 void build(int o, int l, int r) { 14 if (l == r) { 15 t[o] = (Tree) {a[l], a[l]}; 16 return; 17 } 18 int ls = o << 1, rs = o << 1 | 1, mid = (l + r) >> 1; 19 build(ls, l, mid), build(rs, mid + 1, r); 20 t[o] = (Tree) {max(t[ls].mx, t[rs].mx), min(t[ls].mi, t[rs].mi)}; 21 } 22 23 void push(int o) { 24 int ls = o << 1, rs = o << 1 | 1; 25 t[ls].mi += t[o].f, t[ls].mx += t[o].f; 26 t[rs].mi += t[o].f, t[rs].mx += t[o].f; 27 t[ls].f += t[o].f, t[rs].f += t[o].f; 28 t[o].f = 0; 29 } 30 31 void upd(int o, int l, int r, int x) { 32 int ls = o << 1, rs = o << 1 | 1, mid = (l + r) >> 1; 33 if (t[o].f) push(o); 34 if (ql <= l && r <= qr) { 35 t[o].f += x, t[o].mi += x, t[o].mx += x; 36 return; 37 } 38 if (ql <= mid) upd(ls, l, mid, x); 39 if (mid + 1 <= qr) upd(rs, mid + 1, r, x); 40 t[o] = (Tree) {max(t[ls].mx, t[rs].mx), min(t[ls].mi, t[rs].mi)}; 41 } 42 43 int main() { 44 scanf("%d %d", &n, &m); 45 for (int i = 1; i <= n; i++) scanf("%d", &a[i]); 46 build(1, 1, n); 47 for (int i = 1; i <= m; i++) scanf("%d %d", &l[i], &r[i]); 48 for (int i = 1; i <= n; i++) { 49 for (int j = 1; j <= m; j++) { 50 if (i == l[j]) ql = l[j], qr = r[j], upd(1, 1, n, -1); 51 if (i == r[j] + 1) ql = l[j], qr = r[j], upd(1, 1, n, 1); 52 } 53 int mx = t[1].mx, mi = t[1].mi; 54 if (mx - mi > ans) ans = mx - mi, ai = i; 55 } 56 printf("%d\n", ans); 57 for (int i = 1; i <= m; i++) 58 if (l[i] <= ai && ai <= r[i]) tot++; 59 printf("%d\n", tot); 60 for (int i = 1; i <= m; i++) 61 if (l[i] <= ai && ai <= r[i]) printf("%d ", i); 62 return 0; 63 }