……node
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } string s = "CODEFESTIVAL2016",t; void Solve() { cin >> t; int ans = 0; for(int i = 0 ; i < s.length() ; ++i) { if(s[i] != t[i]) ++ans; } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
……c++
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,A,B; char s[MAXN]; void Solve() { read(N);read(A);read(B); scanf("%s",s + 1); int r = 1,all = 0; for(int i = 1 ; i <= N ; ++i) { if(s[i] == 'a') { if(all < A + B) {puts("Yes");++all;} else puts("No"); } else if(s[i] == 'b') { if(all < A + B && r <= B) {puts("Yes");++all;++r;} else puts("No"); } else { puts("No"); } } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
找到p和q中還沒用的最小的一個spa
若是用的是p,q已經用了k個,這個p值能用的就是\(H - k + 1\)這麼多code
用的是q同理ci
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int W,H; int64 p[MAXN],q[MAXN],ans; int id[2][MAXN],c[2]; void Solve() { read(W);read(H); for(int i = 0 ; i < W ; ++i) { id[0][i] = i; read(p[i]); } for(int i = 0 ; i < H ; ++i) { id[1][i] = i; read(q[i]); } sort(id[0],id[0] + W,[](int a,int b){return p[a] < p[b];}); sort(id[1],id[1] + H,[](int a,int b){return q[a] < q[b];}); int p1 = 0,p2 = 0; for(int i = 1 ; i <= W + H ; ++i) { if(p2 >= H || (p1 < W && p[id[0][p1]] < q[id[1][p2]])) { ans += 1LL * (H - c[1] + 1) * p[id[0][p1]]; ++p1;++c[0]; } else { ans += 1LL * (W - c[0] + 1) * q[id[1][p2]]; ++p2;++c[1]; } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
考慮到咱們但願每一個人用盡可能小的東西填滿它,且這個東西不能和以前出現過的大小相等get
咱們先用1把第一我的變成1,而後後面若是出現一個2,則咱們扔的東西都要大於2string
若是出現大於2的數,咱們總有辦法使這個數買了最多\(\lfloor \frac{x}{3} \rfloor\)的狀態下it
若是出現了3以後同理,如此貪心下去便可io
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,a[MAXN]; void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) { read(a[i]); } int pre = 1; int64 ans = a[1] - 1;a[1] = 1; for(int i = 2 ; i <= N ; ++i) { if(a[i] == pre + 1) pre++; else { ans += (a[i] - 1) / (pre + 1); } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
這個我分析了一下,壓縮後的樹高應該是根號級別的,因而愉快的開始卡常,最後就剩三個點死活也過不去了class
翻了一下題解,只要對於trie上每一個點統計在某個字母小於另外一個字母的狀況下,某個點的兒子中的一個兒子是否小於另外一個兒子
由於這種關係只有\(26 * 26\)因此存的下並且很快就過了。。
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } struct node { int to;string s; }; vector<node> to[MAXN * 4]; int N,Q,Ncnt; int ed[MAXN * 4],ans[MAXN],pri[30],siz[MAXN * 4],pla[MAXN]; int dp[MAXN * 4][26][26]; string str[MAXN],order; void insert(int u,int id,int pos) { if(pos == str[id].length()) {ed[u] = id;pla[id] = u;return;} for(auto &t : to[u]) { if(t.s[0] == str[id][pos]) { int l = -1; while(l + 1 < str[id].length() - pos && l + 1 < t.s.length()) { if(str[id][pos + l + 1] == t.s[l + 1]) ++l; else break; } if(l + 1 == t.s.length()) { insert(t.to,id,pos + t.s.length()); return; } int nw = ++Ncnt; to[nw].pb((node){t.to,t.s.substr(l + 1)}); t.to = nw;t.s = t.s.substr(0,l + 1); if(l + 1 == str[id].length() - pos) { ed[nw] = id;pla[id] = nw; } else { to[nw].pb((node){++Ncnt,str[id].substr(pos + l + 1)}); ed[Ncnt] = id; pla[id] = Ncnt; } return; } } to[u].pb((node){++Ncnt,str[id].substr(pos)}); ed[Ncnt] = id;pla[id] = Ncnt;return; } void pre_Process(int u,int all) { if(ed[u]) {ans[ed[u]] = all;siz[u]++;++all;} for(auto t : to[u]) { pre_Process(t.to,all); siz[u] += siz[t.to]; } } void Calc(int u) { for(auto t : to[u]) { memcpy(dp[t.to],dp[u],sizeof(dp[t.to])); for(auto k : to[u]) { if(t.to == k.to) continue; dp[t.to][k.s[0] - 'a'][t.s[0] - 'a'] += siz[k.to]; } } for(auto t : to[u]) { Calc(t.to); } } void Solve() { read(N); Ncnt = 1; for(int i = 1 ; i <= N ; ++i) { cin >> str[i]; insert(1,i,0); } pre_Process(1,0); Calc(1); read(Q); int k; for(int i = 1 ; i <= Q ; ++i) { read(k);cin >> order; int res = 0; for(int j = 0 ; j < 26 ; ++j) { for(int t = j + 1 ; t < 26 ; ++t) { res += dp[pla[k]][order[j] - 'a'][order[t] - 'a']; } } out(res + ans[k] + 1);enter; } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }