Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)
Educational Codeforces Round 90 (Rated for Div. 2)
Donut Shops
思路
分三種情況:
- a==c/ba == c / ba==c/b這個時候兩個的單價是相同的,如果b==1b == 1b==1,也就是a==ca == ca==c,無論買多少數量的東西,這兩個的價格都是一樣的,直接輸出"-1 -1",否則的話,只要輸出"%d -1", %d可以是小于b大于1的任意數字。
- a>c/ba > c / ba>c/b,如果a<ca < ca<c,直接輸出"1 b"即可,否則輸出"-1 b"。
- a<c/ba < c / ba<c/b,這里一定是對于a是最優的,所以可以購買任意數量的a,即"1 -1"也是滿足條件的。
代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _ = read();while(_--) {ll a = read(), b = read(), c = read();if(double(c) / (double)b < a) {if(a < c) printf("%d %d\n", 1, b);else printf("%d %d\n", -1, b);}else if(double(c) / b == a) {if(b == 1) puts("-1 -1");else puts("1 -1");}else {// a < c / bprintf("%d -1\n", 1);}}return 0; }01 Game
思路
直接用棧模擬,得到最多的可消去的數量,然后判斷其是奇數還是偶數即可。
代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _;cin >> _;while(_--) {string str;cin >> str;stack<char> stk;int n = str.size(), num = 0;for(int i = 0; i < n; i++) {if(stk.empty() || stk.top() == str[i]) {stk.push(str[i]);continue;}if(stk.top() != str[i]) {// cout << i << endl;num++;stk.pop();// continue;}}// cout << num << endl;if(num & 1) cout << "DA\n";else cout << "NET\n";}return 0; }Pluses and Minuses
思路
res = 0 for init = 0 to infcur = initok = truefor i = 1 to |s|res = res + 1if s[i] == '+'cur = cur + 1elsecur = cur - 1if cur < 0ok = falsebreakif okbreak-+串可以看作是一個前綴串,當我們的initinitinit與當前綴和相加的時候是負數的時候,我們將在這個點停止,resresres的增加值就是這個點的數組下標,所以我們可以直接遍歷一遍curcurcur停止的點,然后加上這個點的數組下標,因為我們最后跳出循環的條件是當前枚舉的initinitinit是嚴格大于這個數組中的所有非負值,所以我們還需要加上我們最后一遍遍歷數組的值,也就是串的長度。
代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }const int N = 1e6 + 10;char str[N]; int n;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _;cin >> _;while(_--) {scanf("%s", str + 1);n = strlen(str + 1);ll ans = 0, sum = 0;for(int i = 1; i <= n; i++) {if(str[i] == '+') sum++;else sum--;if(sum < 0) {//表示我們當前的cur將會變成負數,這個時候跳出循環,//所以我們要重置0,因為我們在下一個枚舉的cur在這個點一定會是0,然后繼續枚舉下一個停止點。ans += i;sum = 0;}}printf("%lld\n", ans + n);}return 0; }Maximum Sum on Even Positions
思路
容易想到一定是在某一段是"奇偶奇偶……奇偶"或者是"偶奇偶奇……偶奇"這樣的反轉。那么我們就需要貪心的選取這些數段了,也就是奇數 - 偶數是最大的一段,這一步我們容易想到最大字段和的dpdpdp,想到這里我們就應該可以寫出這道題了。
首先我們記錄下所有偶數下標的和sum1sum1sum1,然后通過上面的思想去找到一個最大子段的sum2=奇數?偶數sum2 = 奇數 - 偶數sum2=奇數?偶數的值,然后只需要sum1+sum2sum1 + sum2sum1+sum2就可以得到我們的正確答案了,sum1+sum2=sum1+某一段(奇數?偶數)sum1 + sum2 = sum1 + 某一段(奇數 - 偶數)sum1+sum2=sum1+某一段(奇數?偶數),這個式子中正好去除了我們選取的子段中偶數下標在sum1sum1sum1中的值,然后加上我們選取的子段中奇數下標的值,也就是我們反轉后的答案值。
代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }const int N = 2e5 + 10;ll a[N]; int n;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _;cin >> _;while(_--) {n = read();ll sum1 = 0, sum2 = 0;for(int i = 0; i < n; i++) {a[i] = read();if(!(i & 1)) sum1 += a[i];}ll ans = 0, now = 0, temp = 0;for(int i = 1; i < n; i += 2) {now += a[i] - a[i - 1];ans = max(ans, now);if(now < 0) now = 0;}now = 0, temp = 0;for(int i = 2; i < n; i += 2) {now += a[i - 1] - a[i];ans = max(ans, now);if(now < 0) now = 0;}printf("%lld\n", ans + sum1);}return 0; }Sum of Digits
打表
先對kkk是0的答案自己構造一下,然后就開始打表,分析最大的150,k = 1 時的情況,9 * 8 = 72,所以大概可以斷定我們的打表最大值不超過1e9,然后開始了1e101e101e10復雜度的打表,實際上應該不到這個復雜度,兩分鐘就可以打出表,然后直接cv大法即可。
打表代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }ll ans[151][10];int get(ll x) {int sum = 0;while(x) {sum += x % 10;x /= 10;}return sum; }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);for(int i = 0; i <= 150; i++)for(int j = 0; j <= 9; j++)ans[i][j] = 1e18 + 10;for(int i = 1; i <= 150; i++) {int temp = i;ll Ans = 0, add = 1;while(temp) {if(temp >= 9) {temp -= 9;Ans += add * 9;add *= 10;}else {Ans += add * temp;add *= 10;temp = 0;}}// cout << i << " " << Ans << endl;ans[i][0] = Ans;}int num = 0;for(ll i = 0; i <= 1000000010; i++) {if(i % 10000000 == 0) printf("%lld\n", i / 10000000);//顯示暴力進度。int sum = get(i);for(int j = 1; j <= 9; j++) {sum += get(i + j);if(sum > 150) break;ans[sum][j] = min(ans[sum][j], i);}}for(int i = 0; i <= 150; i++) {for(int j = 0; j <= 9; j++)if(ans[i][j] != 1e18 + 10) printf("%lld, ", ans[i][j]);else printf("-1, ");puts("");}return 0; }AC代碼
#include <bits/stdc++.h> #define mp make_pair #define pb push_backusing namespace std;typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull;const double eps = 1e-7; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }ll ans[151][10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 0, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 0, -1, -1, -1, -1, -1, -1, -1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 2, -1, -1, -1, -1, -1, -1, -1, -1, 6, -1, 1, 0, -1, -1, -1, -1, -1, -1, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 4, 2, -1, -1, -1, -1, -1, -1, -1, 19, 9, -1, 1, 0, -1, -1, -1, -1, -1, 29, 5, -1, -1, -1, -1, -1, -1, -1, -1, 39, 19, 3, -1, -1, -1, -1, -1, -1, -1, 49, 6, -1, -1, -1, -1, -1, -1, -1, -1, 59, 29, -1, 2, -1, -1, -1, -1, -1, -1, 69, 7, 4, 9, 1, 0, -1, -1, -1, -1, 79, 39, -1, -1, -1, -1, -1, -1, -1, -1, 89, 8, -1, -1, -1, -1, -1, -1, -1, -1, 99, 49, 5, 3, -1, -1, -1, -1, -1, -1, 199, 18, -1, 19, 9, -1, -1, -1, -1, -1, 299, 59, -1, 8, 2, -1, -1, -1, -1, -1, 399, 28, 6, -1, -1, 1, 0, -1, -1, -1, 499, 69, -1, 4, -1, -1, -1, -1, -1, -1, 599, 38, -1, 29, 8, -1, -1, -1, -1, -1, 699, 79, 7, 18, 19, 9, -1, -1, -1, -1, 799, 48, -1, 7, 3, -1, -1, -1, -1, -1, 899, 89, -1, 5, -1, -1, -1, -1, -1, -1, 999, 58, 17, 39, 7, 2, -1, -1, -1, -1, 1999, 189, -1, 28, 18, -1, 1, 0, -1, -1, 2999, 68, -1, 17, 29, -1, -1, -1, -1, -1, 3999, 289, 27, 6, 4, 7, 9, -1, -1, -1, 4999, 78, -1, 49, 6, -1, -1, -1, -1, -1, 5999, 389, -1, 38, 17, -1, 8, -1, -1, -1, 6999, 88, 37, 27, 28, 3, -1, -1, -1, -1, 7999, 489, -1, 16, 39, -1, 7, -1, -1, -1, 8999, 98, -1, 59, 5, -1, 2, -1, -1, -1, 9999, 589, 47, 48, 16, 5, 6, 1, 0, -1, 19999, 198, -1, 37, 27, -1, 19, 9, -1, -1, 29999, 689, -1, 26, 38, -1, 5, 8, -1, -1, 39999, 298, 57, 69, 49, 4, 18, 7, -1, -1, 49999, 789, -1, 58, 15, -1, 4, 6, -1, -1, 59999, 398, -1, 47, 26, -1, 17, 5, -1, -1, 69999, 889, 67, 36, 37, 15, 3, 4, -1, -1, 79999, 498, -1, 79, 48, -1, 16, 3, -1, -1, 89999, 989, -1, 68, 59, -1, 29, 2, -1, -1, 99999, 598, 77, 57, 25, 14, 15, 19, 1, 0, 199999, 1989, -1, 46, 36, -1, 28, 18, -1, 1, 299999, 698, -1, 89, 47, -1, 14, 17, -1, 2, 399999, 2989, 87, 78, 58, 25, 27, 16, -1, 3, 499999, 798, -1, 67, 69, -1, 13, 15, -1, 4, 599999, 3989, -1, 56, 35, -1, 26, 14, -1, 5, 699999, 898, 97, 189, 46, 24, 39, 13, -1, 6, 799999, 4989, -1, 88, 57, -1, 25, 12, -1, 7, 899999, 998, -1, 77, 68, -1, 38, 29, -1, 8, 999999, 5989, 197, 66, 79, 35, 24, 28, 11, 9, 1999999, 1998, -1, 289, 45, -1, 37, 27, -1, 10, 2999999, 6989, -1, 188, 56, -1, 23, 26, -1, 11, 3999999, 2998, 297, 87, 67, 34, 36, 25, -1, 12, 4999999, 7989, -1, 76, 78, -1, 49, 24, -1, 13, 5999999, 3998, -1, 389, 89, -1, 35, 23, -1, 14, 6999999, 8989, 397, 288, 55, 45, 48, 22, -1, 15, 7999999, 4998, -1, 187, 66, -1, 34, 39, -1, 16, 8999999, 9989, -1, 86, 77, -1, 47, 38, -1, 17, 9999999, 5998, 497, 489, 88, 44, 33, 37, 21, 18, 19999999, 19989, -1, 388, 189, -1, 46, 36, -1, 19, 29999999, 6998, -1, 287, 65, -1, 59, 35, -1, 20, 39999999, 29989, 597, 96, 76, 55, 45, 34, -1, 21, 49999999, 7998, -1, 589, 87, -1, 58, 33, -1, 22, 59999999, 39989, -1, 488, 188, -1, 44, 32, -1, 23, 69999999, 8998, 697, 387, 289, 54, 57, 49, -1, 24, 79999999, 49989, -1, 196, 75, -1, 43, 48, -1, 25, 89999999, 9998, -1, 689, 86, -1, 56, 47, -1, 26, 99999999, 59989, 797, 588, 187, 65, 69, 46, 31, 27, 199999999, 19998, -1, 487, 288, -1, 55, 45, -1, 28, 299999999, 69989, -1, 296, 389, -1, 68, 44, -1, 29, 399999999, 29998, 897, 789, 85, 64, 54, 43, -1, 30, 499999999, 79989, -1, 688, 186, -1, 67, 42, -1, 31, 599999999, 39998, -1, 587, 287, -1, 53, 59, -1, 32, 699999999, 89989, 997, 396, 388, 75, 66, 58, -1, 33, 799999999, 49998, -1, 889, 489, -1, 79, 57, -1, 34, 899999999, 99989, -1, 788, 95, -1, 65, 56, -1, 35, 999999999, 59998, 1997, 687, 286, 74, 78, 55, 41, 36, 1999999999, 199989, -1, 496, 387, -1, 64, 54, -1, 37, 2999999999, 69998, -1, 989, 488, -1, 77, 53, -1, 38, 3999999999, 299989, 2997, 888, 589, 85, 63, 52, -1, 39, 4999999999, 79998, -1, 787, 195, -1, 76, 69, -1, 40, 5999999999, 399989, -1, 596, 386, -1, 89, 68, -1, 41, 6999999999, 89998, 3997, 1989, 487, 84, 75, 67, -1, 42, 7999999999, 499989, -1, 988, 588, -1, 88, 66, -1, 43, 8999999999, 99998, -1, 887, 689, -1, 74, 65, -1, 44, 9999999999, 599989, 4997, 696, 295, 185, 87, 64, 51, 45, 19999999999, 199998, -1, 2989, 486, -1, 73, 63, -1, 46, 29999999999, 699989, -1, 1988, 587, -1, 86, 62, -1, 47, 39999999999, 299998, 5997, 987, 688, 94, 189, 79, -1, 48, 49999999999, 799989, -1, 796, 789, -1, 85, 78, -1, 49, 59999999999, 399998, -1, 3989, 395, -1, 188, 77, -1, 50, 69999999999, 899989, 6997, 2988, 586, 285, 84, 76, -1, 51, 79999999999, 499998, -1, 1987, 687, -1, 187, 75, -1, 52, 89999999999, 999989, -1, 896, 788, -1, 83, 74, -1, 53, 99999999999, 599998, 7997, 4989, 889, 194, 186, 73, 61, 54, 199999999999, 1999989, -1, 3988, 495, -1, 289, 72, -1, 55, 299999999999, 699998, -1, 2987, 686, -1, 185, 89, -1, 56, 399999999999, 2999989, 8997, 996, 787, 385, 288, 88, -1, 57, 499999999999, 799998, -1, 5989, 888, -1, 184, 87, -1, 58, 599999999999, 3999989, -1, 4988, 989, -1, 287, 86, -1, 59, 699999999999, 899998, 9997, 3987, 595, 294, 93, 85, -1, 60, 799999999999, 4999989, -1, 1996, 786, -1, 286, 84, -1, 61, 899999999999, 999998, -1, 6989, 887, -1, 389, 83, -1, 62, 999999999999, 5999989, 19997, 5988, 988, 485, 285, 82, 71, 63, 1999999999999, 1999998, -1, 4987, 1989, -1, 388, 189, -1, 64, 2999999999999, 6999989, -1, 2996, 695, -1, 284, 188, -1, 65, 3999999999999, 2999998, 29997, 7989, 886, 394, 387, 187, -1, 66, 4999999999999, 7999989, -1, 6988, 987, -1, 193, 186, -1, 67, 5999999999999, 3999998, -1, 5987, 1988, -1, 386, 185, -1, 68, 6999999999999, 8999989, 39997, 3996, 2989, 585, 489, 184, -1, 69, 7999999999999, 4999998, -1, 8989, 795, -1, 385, 183, -1, 70, 8999999999999, 9999989, -1, 7988, 986, -1, 488, 92, -1, 71, 9999999999999, 5999998, 49997, 6987, 1987, 494, 384, 289, 81, 72, 19999999999999, 19999989, -1, 4996, 2988, -1, 487, 288, -1, 73, 29999999999999, 6999998, -1, 9989, 3989, -1, 293, 287, -1, 74, 39999999999999, 29999989, 59997, 8988, 895, 685, 486, 286, -1, 75, 49999999999999, 7999998, -1, 7987, 1986, -1, 589, 285, -1, 76, 59999999999999, 39999989, -1, 5996, 2987, -1, 485, 284, -1, 77, 69999999999999, 8999998, 69997, 19989, 3988, 594, 588, 283, -1, 78, 79999999999999, 49999989, -1, 9988, 4989, -1, 484, 192, -1, 79, 89999999999999, 9999998, -1, 8987, 995, -1, 587, 389, -1, 80, 99999999999999, 59999989, 79997, 6996, 2986, 785, 393, 388, 91, 81, 199999999999999, 19999998, -1, 29989, 3987, -1, 586, 387, -1, 82, 299999999999999, 69999989, -1, 19988, 4988, -1, 689, 386, -1, 83, 399999999999999, 29999998, 89997, 9987, 5989, 694, 585, 385, -1, 84, 499999999999999, 79999989, -1, 7996, 1995, -1, 688, 384, -1, 85, 599999999999999, 39999998, -1, 39989, 3986, -1, 584, 383, -1, 86, 699999999999999, 89999989, 99997, 29988, 4987, 885, 687, 292, -1, 87, 799999999999999, 49999998, -1, 19987, 5988, -1, 493, 489, -1, 88, 899999999999999, 99999989, -1, 8996, 6989, -1, 686, 488, -1, 89, 999999999999999, 59999998, 199997, 49989, 2995, 794, 789, 487, 191, 90, 1999999999999999, 199999989, -1, 39988, 4986, -1, 685, 486, -1, 181, 2999999999999999, 69999998, -1, 29987, 5987, -1, 788, 485, -1, 182, 3999999999999999, 299999989, 299997, 9996, 6988, 985, 684, 484, -1, 183, 4999999999999999, 79999998, -1, 59989, 7989, -1, 787, 483, -1, 184, 5999999999999999, 399999989, -1, 49988, 3995, -1, 593, 392, -1, 185, 6999999999999999, 89999998, 399997, 39987, 5986, 894, 786, 589, -1, 186, 7999999999999999, 499999989, -1, 19996, 6987, -1, 889, 588, -1, 187, 8999999999999999, 99999998, -1, 69989, 7988, -1, 785, 587, -1, 188, 9999999999999999, 599999989, 499997, 59988, 8989, 1985, 888, 586, 291, 189, 19999999999999999, 199999998, -1, 49987, 4995, -1, 784, 585, -1, 190, 29999999999999999, 699999989, -1, 29996, 6986, -1, 887, 584, -1, 281, 39999999999999999, 299999998, 599997, 79989, 7987, 994, 693, 583, -1, 282, 49999999999999999, 799999989, -1, 69988, 8988, -1, 886, 492, -1, 283, 59999999999999999, 399999998, -1, 59987, 9989, -1, 989, 689, -1, 284, 69999999999999999, 899999989, 699997, 39996, 5995, 2985, 885, 688, -1, 285 };int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int _ = read();while(_--) {int n = read(), k = read();printf("%lld\n", ans[n][k]);}return 0; }總結
以上是生活随笔為你收集整理的Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄金微针多少钱做一次
- 下一篇: 血热妄行如何调理