The game feeling good quality, the question people really conscience, sample data to less detailed.
This time I also first met ILE ( Idleness limit exceeded Number of ), original fflushu (stdout) is to be used after each output ......
A.Temporarily unavailable
Title effect: There are a number of axes, from a point went point b, there is a network where the point c, the coverage radius r of the circle. I asked how much time have a network coverage.
Because it is on the number line, so where there is network coverage (c - r, c + r), as long as the reduction look like, note that in some regions may run out of place.
code show as below:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #define rep(x, l, r) for(int x = l; x <= r; x++) 7 #define repd(x, r, l) for(int x = r; x >= l; x--) 8 #define clr(x, y) memset(x, y, sizeof(x)) 9 #define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long ll; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int INF = 1 << 30; 21 const int p = 1000000009; 22 int lowbit(int x){ return x & (-x);} 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;} 24 25 int main(){ 26 int t; 27 scanf("%d", &t); 28 while(t--){ 29 int a, b, c, r; 30 scanf("%d%d%d%d", &a, &b, &c, &r); 31 printf("%d\n", max(a, b) - min(a, b) - max(0, min(max(a, b), c + r) - max(min(a, b), c - r))); 32 } 33 return 0; 34 }
B1.K for the Price of One(Easy Version)
Subject to the effect: there are n commodities, each commodity value A i , now there is an active offer, buy one item can be selected k - 1 a value of less than or equal to the free access of its goods (or none are selected, be sure to either option k - 1 a), seeking a total of k coins can buy many items. In this problem the k = 2.
Not difficult to find, buy a few items that must be the most expensive. We set state DP [i] [ 0 / . 1 ] denotes the i-th item is selected or not selected minimum cost.
If you do not chose the i, i - 1 bound to the election, if the election of the i-th, i - 1 one can not vote for the election can obtain transfer equation.
Finally, find the biggest dp [i] [ 1 ] less than or equal p is the answer.
code show as below:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #define rep(x, l, r) for(int x = l; x <= r; x++) 7 #define repd(x, r, l) for(int x = r; x >= l; x--) 8 #define clr(x, y) memset(x, y, sizeof(x)) 9 #define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 200005 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long ll; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int INF = 1 << 30; 21 const int p = 1000000009; 22 int lowbit(int x){ return x & (-x);} 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;} 24 25 int a[MAXN], dp[MAXN][2]; 26 27 int main(){ 28 int t; 29 scanf("%d", &t); 30 rep(times, 1, t){ 31 int n, p, k; 32 scanf("%d%d%d", &n, &p, &k); 33 rep(i, 1, n) scanf("%d", &a[i]); 34 sort(a + 1, a + n + 1); 35 rep(i, 1, n){ 36 dp[i][0] = dp[i - 1][1]; 37 dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + a[i]; 38 } 39 int ans = 0; 40 rep(i, 1, n) 41 if(dp[i][1] <= p) ans = i; 42 else break; 43 printf("%d\n", ans); 44 } 45 return 0; 46 }
B2.K for the Price of One(Hard Version)
Subject to the effect: there are n commodities, each commodity value of A i now have a preferential activity, buy one item can be selected k - 1 a value of less than or equal to the free access of its goods (or none are selected, must choose either k - 1 a), seeking a total of k coins can buy many items. In this problem the k <= n.
This question is found to B1 finished writing complicated, but in fact these two questions as the code of ......
The main idea is greedy + recurrence.
Obviously, if the i buy items worth less than it is bound to choose (not white do not, and certainly to most expensive).
Then we obtain the recursion formula F [i] = F [i - K] + A [i] , it is noted that if i is less than equal to k, the total cost required for the front of the i-th item.
code show as below:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #define rep(x, l, r) for(int x = l; x <= r; x++) 7 #define repd(x, r, l) for(int x = r; x >= l; x--) 8 #define clr(x, y) memset(x, y, sizeof(x)) 9 #define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 200005 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long ll; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int INF = 1 << 30; 21 const int p = 1000000009; 22 int lowbit(int x){ return x & (-x);} 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;} 24 25 int a[MAXN], dp[MAXN]; 26 27 int main(){ 28 int t; 29 scanf("%d", &t); 30 rep(times, 1, t){ 31 int n, p, k; 32 scanf("%d%d%d", &n, &p, &k); 33 rep(i, 1, n) scanf("%d", &a[i]); 34 sort(a + 1, a + n + 1); 35 int ans = 0; 36 rep(i, 1, n){ 37 if(i < k) dp[i] = dp[i - 1] + a[i]; 38 else dp[i] = dp[i - k] + a[i]; 39 } 40 rep(i, 1, n) 41 if(dp[i] <= p) ans = i; 42 printf("%d\n", ans); 43 } 44 return 0; 45 }
C.Petya and Exam
Subject to the effect: Petya will participate in an exam, the exam time starts from the point 0, the T end. There are n exam questions, divided into two types, simple (it takes a time to finish) title and difficulties (the time it takes to finish b) of title (a <= b), namely at point x time to start doing this questions, will be completed or x + a x + b time. Now each question will be a point in time t i become must complete, Petya 0 ~ T may be in any one point in time to leave, there is no need to be done to complete the title, he will receive 0 points if the leave, otherwise they will get him score completed topics. Seeking his biggest score can get.
We can get a greedy strategy, if at the time i left and i + 1 does not have to complete the subject, then point i leave certainly not as good as in i + 1 time left, so we just compare all t i - 1 leave can get the maximum score.
Only need a point in time t i after ordering, you can record the time required to complete the title and takes time .
另外,对于每一个点,在完成所有需要完成的题目后,肯定要先去做简单的题目,若还有时间再去做困难的题目。
代码如下
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #define rep(x, l, r) for(int x = l; x <= r; x++) 7 #define repd(x, r, l) for(int x = r; x >= l; x--) 8 #define clr(x, y) memset(x, y, sizeof(x)) 9 #define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 200005 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long ll; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int INF = 1 << 30; 21 const int p = 1000000009; 22 int lowbit(int x){ return x & (-x);} 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;} 24 25 pii pro[MAXN]; 26 int num[MAXN]; 27 28 int main(){ 29 int q; 30 scanf("%d", &q); 31 rep(times, 1, q){ 32 int n, T, a, b; 33 scanf("%d%d%d%d", &n, &T, &a, &b); 34 int tot0 = 0, tot1 = 0; 35 rep(i, 1, n){ 36 scanf("%d", &num[i]); 37 if(!num[i]) tot0++; 38 else tot1++; 39 } 40 rep(i, 1, n){ 41 int x; 42 scanf("%d", &x); 43 pro[i] = mp(x, num[i]); 44 } 45 pro[++n] = mp(T + 1, 0); 46 sort(pro + 1, pro + n + 1); 47 int ans = 0, sta0 = 0, sta1 = 0; 48 rep(i, 1, n){ 49 if(sta0 > T) break; 50 if(pro[i - 1].fi != pro[i].fi){ 51 int tim = pro[i].fi - 1; 52 tim -= sta0; 53 int res = sta1; 54 if(tim >= 0){ 55 res += min(tim / a, tot0); 56 tim -= min(tim / a, tot0) * a; 57 res += min(tim / b, tot1); 58 ans = max(ans, res); 59 } 60 } 61 if(pro[i].se){ 62 tot1--; 63 sta0 += b; 64 } 65 else{ 66 tot0--; 67 sta0 += a; 68 } 69 sta1++; 70 } 71 printf("%d\n", ans); 72 } 73 return 0; 74 }
D.Enchanted Artifact
题目大意:本题为交互题。有一个字符串s,只由字符'a'和'b'组成。每次你可以询问一个字符串,它会返回这两个字符串的编辑距离。为一个字符串经过修改,删除或插入操作得到另一个字符串,两个字符串编辑距离的定义为最小的操作次数,若返回值为0,那么就是字符串s。让你在n + 2操作内得出字符串s(n为字符串s的长度,未知)。
人生中第一次对交互题有想法,但是是错的想法……
一开始我认为输入'a'和'b',若是原字符串有'a'字符,那么返回的是长度n - 1,否则返回的是长度n,那么原字符串的长度n为返回两值的最小值+1。
然后询问一个由1个'b'和 n - 1 个'a'组成的字符串,共有n种,返回的数一定是字符串中b的个数加1或减1,即该位是否为b,然后因为在询问n +1次后一定要给出字符串s,那么通过前n - 1推出最后一位是否是'b'。
这个方法看上去没问题,但是发现了一组反例。
s字符串为"baaab"
询问的字符串为"aaaba"
根据我的思路返回数应该是3,即字符串中不相同的个数,但是这个数据返回了2。
只需要在首位插入'b',在末位插入'a'即可。
然后我就傻掉了,去看了下题解。
发现题解的思路是先输入300个'a',再输入300个'b',返回的数分别是300 - 'a'的个数以及300 - 'b'的个数,那么就得到了原字符串中'a'和'b'的个数以及字符串的长度。然后将答案串设为全'a',对于每一位将该位改为'b',如果返回值小于当前的编辑长度(一开始全'a'的编辑长度就是字符'b'的个数),那么答案的这一位一定是'b'。但是最多询问n + 2次,那么最后一位也只能靠前面答案推出,即若当前编辑长度为1那么最后一位是'b',否则为'a'。
乍一看这个题解思路和我的差不多,但是它不会像我的代码一样出现反例。
为什么呢,因为对于第i位改为'b'时,它前面的字符已经是和字符串s相同了,而我这个反例的最少的编辑操作是将'b'前面的一段往后移动一位,再插入一位比只修改操作要优。但是由于前i - 1位已经相同了就不会有这种操作,那么后面就一定是修改操作。
另外题解中一开始求长度的操作也是优于我的方法的,我的方法很难得出是插入操作还是存在修改操作,所以在n = 2的情况下很难直接得出答案,而题解直接得出了字符串s中'a'和'b'的个数,可以直接给出。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #define rep(x, l, r) for(int x = l; x <= r; x++) 7 #define repd(x, r, l) for(int x = r; x >= l; x--) 8 #define clr(x, y) memset(x, y, sizeof(x)) 9 #define all(x) x.begin(), x.end() 10 #define pb push_back 11 #define mp make_pair 12 #define MAXN 13 #define fi first 14 #define se second 15 #define SZ(x) ((int)x.size()) 16 using namespace std; 17 typedef long long ll; 18 typedef vector<int> vi; 19 typedef pair<int, int> pii; 20 const int INF = 1 << 30; 21 const int p = 1000000009; 22 int lowbit(int x){ return x & (-x);} 23 int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;} 24 25 int judge(string st){ 26 puts(st.c_str()); 27 fflush(stdout); 28 int ans; 29 scanf("%d", &ans); 30 if(!ans) exit(0); 31 return ans; 32 } 33 34 int main(){ 35 int n = 300; 36 int lena = n - judge(string(n, 'a')), 37 lenb = n - judge(string(n, 'b')); 38 int len = lena + lenb; 39 string ans = string(len, 'a'); 40 int res = lenb; 41 rep(i, 0, len - 2){ 42 ans[i] = 'b'; 43 int s = judge(ans); 44 if(s > res) ans[i] = 'a'; 45 else res = s; 46 } 47 if(res) ans[len - 1] = 'b'; 48 judge(ans); 49 return 0; 50 }