直接遞推就好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; int dp[MAXN]; void Solve() { cin >> s; dp[0] = 1; for(int i = 1 ; i <= s.length() ; ++i) { if(i >= 5) { dp[i] |= (dp[i - 5] && s.substr(i - 5,5) == "dream"); dp[i] |= (dp[i - 5] && s.substr(i - 5,5) == "erase"); } if(i >= 6) { dp[i] |= (dp[i - 6] && s.substr(i - 6,6) == "eraser"); } if(i >= 7) { dp[i] |= (dp[i - 7] && s.substr(i - 7,7) == "dreamer"); } } if(dp[s.length()]) puts("YES"); else puts("NO"); } 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 200005 //#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); } map<pii,int> zz; int N,K,L; int fa[MAXN],col[MAXN][2],tot; int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } void Solve() { read(N);read(K);read(L); for(int i = 1 ; i <= N ; ++i) fa[i] = i; int a,b; for(int i = 1 ; i <= K ; ++i) { read(a);read(b); fa[getfa(a)] = getfa(b); } tot = 0; for(int i = 1 ; i <= N ; ++i) { if(getfa(i) == i) col[i][0] = ++tot; } for(int i = 1 ; i <= N ; ++i) col[i][0] = col[getfa(i)][0]; for(int i = 1 ; i <= N ; ++i) fa[i] = i; for(int i = 1 ; i <= L ; ++i) { read(a);read(b); fa[getfa(a)] = getfa(b); } tot = 0; for(int i = 1 ; i <= N ; ++i) { if(getfa(i) == i) col[i][1] = ++tot; } for(int i = 1 ; i <= N ; ++i) col[i][1] = col[getfa(i)][1]; for(int i = 1 ; i <= N ; ++i) { zz[mp(col[i][0],col[i][1])]++; } for(int i = 1 ; i <= N ; ++i) { out(zz[mp(col[i][0],col[i][1])]);space; } enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
曼哈頓轉切比雪夫spa
而後把點對排序,每一個點用橫座標相連就是code
\(x_{a} - x_{b} = D\)排序
而且$ -D<= y_{a} - y_{b} <= D$ci
這是一段區間,咱們能夠把區間裏的點兩兩相連,再和a點相連,是等價的get
而後記錄每一個點連出去的邊有多少,和初始的兩個點連通的全部點之間的邊數加起來就是答案string
#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,next; }E[MAXN * 10]; int head[MAXN],sumE; int N,a,b; int x[MAXN],y[MAXN],D,id[MAXN],cnt[MAXN]; pii p[MAXN],tmp[MAXN]; int st[MAXN],ed[MAXN]; int fa[MAXN]; int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } void Merge(int x,int y) { fa[getfa(x)] = getfa(y); } void AddE(int on) { for(int i = 1 ; i <= N ; ++i) { id[i] = i; } sort(id + 1,id + N + 1,[](int a,int b){return p[a] < p[b];}); memset(st,0,sizeof(st));memset(ed,0,sizeof(ed)); for(int i = 1 ; i <= N ; ++i) { tmp[i] = p[id[i]]; } for(int i = 1 ; i <= N ; ++i) { int l = lower_bound(tmp + 1,tmp + N + 1,mp(tmp[i].fi + D,tmp[i].se - D + on)) - tmp; int r = upper_bound(tmp + 1,tmp + N + 1,mp(tmp[i].fi + D,tmp[i].se + D - on)) - tmp - 1; if(l <= r) { st[l]++;ed[r]++; Merge(id[i],id[l]); cnt[id[i]] += r - l + 1; } } int pre = 0; for(int i = 1 ; i < N ; ++i) { pre += st[i];pre -= ed[i]; if(pre) Merge(id[i],id[i + 1]); } } void Solve() { read(N);read(a);read(b); for(int i = 1 ; i <= N ; ++i) { read(x[i]);read(y[i]); fa[i] = i; } D = abs(x[a] - x[b]) + abs(y[a] - y[b]); for(int i = 1 ; i <= N ; ++i) p[i] = mp(x[i] + y[i],x[i] - y[i]); AddE(0); for(int i = 1 ; i <= N ; ++i) swap(p[i].fi,p[i].se); AddE(1); int64 ans = 0; for(int i = 1 ; i <= N ; ++i) { if(getfa(i) == getfa(a)) ans += cnt[i]; } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
就是對於一個區間,記錄能用a個0和b個1it
而後對於某個位置填0仍是填1class
因爲能用的數是總和是肯定的,咱們只要記錄當前有a個0沒用就能夠了
#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 3005 //#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); } const int MOD = 1000000007; int ri[MAXN],f[2][MAXN]; int N,M; char s[MAXN]; int inc(int a,int b) { return a + b >= MOD ? a + b - MOD : a + b; } int mul(int a,int b) { return 1LL * a * b % MOD; } void update(int &x,int y) { x = inc(x,y); } void Solve() { read(N);read(M); scanf("%s",s + 1); int l,r; for(int i = 1 ; i <= M ; ++i) { read(l);read(r); ri[l] = max(ri[l],r); } int p = 0,all = 0; int cur = 0; f[cur][0] = 1; for(int i = 1 ; i <= N ; ++i) { if(p < i - 1) {p = i - 1;} if(ri[i] > p) { all += ri[i] - p; int a = 0,b = 0; for(int j = p + 1 ; j <= ri[i] ; ++j) { if(s[j] == '0') ++a; else ++b; } p = ri[i]; memset(f[cur ^ 1],0,sizeof(f[cur ^ 1])); for(int j = a ; j <= N ; ++j) f[cur ^ 1][j] = f[cur][j - a]; cur ^= 1; } if(!all) continue; memset(f[cur ^ 1],0,sizeof(f[cur ^ 1])); for(int j = 0 ; j <= all ; ++j) { if(j < all) update(f[cur ^ 1][j],f[cur][j]); if(j >= 1) update(f[cur ^ 1][j - 1],f[cur][j]); } --all; cur ^= 1; } out(f[cur][0]);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }