A Vitaly and Strings
题意:给定长度相等的字符串s,t,且s的字典序小于t,求一个字符串q字典序大于s小于t。
分析:将字符串看做26进制的数,对s”+1“即可。
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define esp 1e-14 #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define low(x) ((x)&(-(x))) #define sz(x) ((int)((x).size())) #define pf(x) ((x)*(x)) #define pb push_back #define pi acos(-1.0) #define in freopen("solve_in.txt", "r", stdin); #define bug(x) printf("Line : %u >>>>>>\n", (x)); #define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; #define inf 0x0f0f0f0f using namespace std; typedef long long LL; typedef unsigned US; typedef pair<int, int> PII; typedef map<PII, int> MPS; typedef MPS::iterator IT; char sa[111], sb[111]; int main(){ // in cin >> sa >> sb; int n = strlen(sa); int ok = 0; for(int i = n-1; i >= 0; i--){ if(sa[i] != ‘z‘) { sa[i] = sa[i] + 1; ok = 1; break; }else sa[i] = ‘a‘; } if(ok) { ok = strcmp(sa, sb) < 0; } if(ok) cout << sa << endl; else puts("No such string"); return 0; }
B Tanya and Postcard
题意:给定s和t,t的长度>=s,t中选出一些字符构成一个字符串,要求对应位置和s完全相同的尽量多,
然后剩下的要求对应位置仅仅大小写不同的尽量多。
分析:用t优先给s中字符找到完全相同的字符匹配,然后再利用大小写不同的字符匹配。
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define esp 1e-14 #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define low(x) ((x)&(-(x))) #define sz(x) ((int)((x).size())) #define pf(x) ((x)*(x)) #define pb push_back #define pi acos(-1.0) #define in freopen("solve_in.txt", "r", stdin); #define bug(x) printf("Line : %u >>>>>>\n", (x)); #define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; #define inf 0x0f0f0f0f using namespace std; typedef long long LL; typedef unsigned US; typedef pair<int, int> PII; typedef map<PII, int> MPS; typedef MPS::iterator IT; const int maxn = (int)2e5 + 100; char sa[maxn], sb[maxn]; int cntA[30][2], cntB[30][2]; int main(){ // in int ans1 = 0, ans2 = 0; scanf("%s%s", sa, sb); int n = strlen(sa); int m = strlen(sb); for(int i = 0; i < n; i++){ if(sa[i] >= ‘a‘ && sa[i] <= ‘z‘){ cntA[sa[i]-‘a‘][0]++; }else{ cntA[sa[i]-‘A‘][1]++; } } for(int i = 0; i < m; i++){ if(sb[i] >= ‘a‘ && sb[i] <= ‘z‘){ cntB[sb[i]-‘a‘][0]++; }else{ cntB[sb[i]-‘A‘][1]++; } } for(int i = 0; i < 26; i++){ int tmp1, tmp2; ans1 += (tmp1 = min(cntA[i][0], cntB[i][0])) + (tmp2 = min(cntA[i][1], cntB[i][1])); cntA[i][0] -= tmp1; cntB[i][0] -= tmp1; cntA[i][1] -= tmp2; cntB[i][1] -= tmp2; ans2 += min(cntA[i][1], cntB[i][0]); ans2 += min(cntA[i][0], cntB[i][1]); } printf("%d %d\n", ans1, ans2); return 0; }
C Anya and Smartphone
题意:n个程序,每个屏幕上最多k个,给定放置顺序,以及依次运行的m个程序,每次运行后位置向前移动一位,
位于第i个屏幕的程序需要i此操作。
分析:直接模拟每次操作,运行后交换一下和之前的程序的位置。
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define esp 1e-14 #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define low(x) ((x)&(-(x))) #define sz(x) ((int)((x).size())) #define pf(x) ((x)*(x)) #define pb push_back #define pi acos(-1.0) #define in freopen("solve_in.txt", "r", stdin); #define bug(x) printf("Line : %u >>>>>>\n", (x)); #define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; #define inf 0x0f0f0f0f using namespace std; typedef long long LL; typedef unsigned US; typedef pair<int, int> PII; typedef map<PII, int> MPS; typedef MPS::iterator IT; const int maxn = (int)1e5 + 10; int a[maxn], pos[maxn]; int main(){ // in int n, m, k; LL ans = 0; cin >> n >> m >> k; for(int i = 1; i <= n; i++){ scanf("%d", a+i); pos[a[i]] = i; } for(int i = 1; i <= m; i++){ int u; scanf("%d", &u); ans += pos[u]/k + (pos[u]%k != 0); if(pos[u] != 1) { swap(a[pos[u]-1], a[pos[u]]); int t = pos[u]; pos[a[t]] = t; pos[a[t-1]] = t-1; } } printf("%I64d\n", ans); return 0; }
D Ilya and Escalator
题意:n个人排队依次上电梯,每个人进入或继续等待电梯的概率分别为p,1-p,每秒钟最多只有队首的人进入电梯,
求t秒后在电梯上的人数期望值。
分析:dp[t][m] 表示t秒时有m个人上电梯的概率,转移:dp[t][m] = dp[t-1][m]*(1-p)+dp[t-1][m-1]*p,注意一下边界。
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define esp 1e-14 #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define low(x) ((x)&(-(x))) #define sz(x) ((int)((x).size())) #define pf(x) ((x)*(x)) #define pb push_back #define pi acos(-1.0) #define in freopen("solve_in.txt", "r", stdin); #define bug(x) printf("Line : %u >>>>>>\n", (x)); #define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; #define inf 0x0f0f0f0f using namespace std; typedef long long LL; typedef unsigned US; typedef pair<int, int> PII; typedef map<PII, int> MPS; typedef MPS::iterator IT; const int maxn = 2000 + 10; double dp[maxn][maxn]; int main() { // in int n, m; double p; scanf("%d%lf%d", &n, &p, &m); dp[0][0] = 1.0; for(int i = 1; i <= m; i++) for(int j = 0; j <= min(i, n); j++) { if(j != 0) dp[i][j] += dp[i-1][j-1]*p; if(j == n) dp[i][j] += dp[i-1][j]; else dp[i][j] += dp[i-1][j]*(1-p); } double ans = 0; for(int i = 0; i <= n; i++) ans += dp[m][i]*i; printf("%.12f\n", ans); return 0; }
E Arthur and Questions
题意:给定数组a[],长度n <= 1e5,以及k,要求满足 (a1 + a2 ... + ak, a2 + a3 + ... + ak + 1, ..., an - k + 1 + an - k + 2 + ... + an)构成递增
序列,其中有些ai是未知的,要求满足条件的a[i]序列且|a[i]的和尽量小。
分析:由上面序列递增容易得到:
a1 < ak+1 < a2k+1 < ... < apk+1
a2 < ak+2 < a2k+2 < ... < aqk+1
....
ak < a2K < a3k < ... < ark
每个不等式组独立,以a1 < ak+1 < a2k+1 < ... < apk+1为例,考虑其中两个值确定(不为?)的aik+1及ajk+1,
之间的alk+1的取值未定,那么alk+1取值肯定在aik+1~ajk+1之间,记做[l, r],数的个数必须满足r-l+1>=j-i-1,记num = j-i-1。
分为以下几种情况:
r <= 0 : 将alk+1取r-num+1, r-num+2, ....r即可;
l >= 0 : 将alk+1取l, l+1, l+2,........l+num-1即可;
l <0<r :按照0,±1,±2,±3.....±n等,注意边界范围不足时进行适当处理。
对于仍然为?的a[i]取0即可。
代码:
1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define mp make_pair 4 #define esp 1e-14 5 #define lson l, m, rt<<1 6 #define rson m+1, r, rt<<1|1 7 #define low(x) ((x)&(-(x))) 8 #define sz(x) ((int)((x).size())) 9 #define pf(x) ((x)*(x)) 10 #define pb push_back 11 #define pi acos(-1.0) 12 #define in freopen("solve_in.txt", "r", stdin); 13 #define bug(x) printf("Line : %u >>>>>>\n", (x)); 14 #define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; 15 #define inf 0x0f0f0f0f 16 17 18 using namespace std; 19 typedef long long LL; 20 typedef unsigned US; 21 typedef pair<int, int> PII; 22 typedef map<PII, int> MPS; 23 typedef MPS::iterator IT; 24 const int maxn = (int)1e5 + 10; 25 const int INF = (int)2e9; 26 int a[maxn]; 27 28 int main() { 29 // in 30 int n, k; 31 cin >> n >> k; 32 getchar(); 33 for(int i = 1; i <= n; i++) { 34 char ch; 35 while((ch = getchar()) == ‘ ‘); 36 if(isdigit(ch) || ch == ‘-‘) { 37 ungetc(ch, stdin); 38 scanf("%d", a+i); 39 } else a[i] = INF; 40 } 41 a[0] = -(int)1e9 - maxn; 42 a[n+1] = (int)1e9 + maxn; 43 int ok = 1; 44 for(int st = 1; st <= k && ok; st++) { 45 if(st+k > n) break; 46 vector<int> index; 47 index.pb(0); 48 for(int now = st; now <= n; now += k) 49 index.pb(now); 50 index.pb(n+1); 51 int first = 0, next = 1; 52 53 while(next < sz(index) && ok) { 54 while(a[index[next]] == INF) 55 next++; 56 57 int l = a[index[first]]; 58 int r = a[index[next]]; 59 if(r-l-1 < next-first-1) { 60 ok = 0; 61 break; 62 } 63 l++, r--; 64 vector<int> val; 65 int num = next-first-1; 66 if(l >= 0) { 67 while(num--) { 68 val.pb(l+num); 69 } 70 } else if(r <= 0) { 71 while(num--) { 72 val.pb(r-num); 73 } 74 } else { 75 int v = 0; 76 for(int p = 0; p < num; p++) { 77 int parity = p&1; 78 if(p) { 79 if(parity) { 80 v = -v+1; 81 if(v > r) { 82 v = -v; 83 while(p < num) { 84 val.pb(v); 85 v--; 86 p++; 87 } 88 break; 89 } 90 } else { 91 v = -v; 92 if(v < l) { 93 v = -v+1; 94 while(p < num) { 95 val.pb(v); 96 v++; 97 p++; 98 } 99 break; 100 } 101 } 102 } 103 val.pb(v); 104 } 105 } 106 107 sort(val.begin(), val.end()); 108 int cnt = 0; 109 110 while(++first < next) { 111 a[index[first]] = val[cnt++]; 112 } 113 next++; 114 } 115 } 116 if(ok) { 117 for(int i = 1; i <= n; i++) 118 printf("%d%c", a[i] == INF ? 0 : a[i], i == n ? ‘\n‘ : ‘ ‘); 119 } else puts("Incorrect sequence"); 120 121 return 0; 122 }
F Pasha and Pipe
代码: