QAQAQAQAQ
D题sb题没写出来(大雾)
QAQAQAQ
差点掉ratingQAQ
c题我能再wa多次吗,就打错个max的转移啊!QAQ
题意:给你a和b,问你a是否小于等于b-2
这。。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } int main() { int n=getint(), ans=0; while(n--) { int a=getint(), b=getint(); if(b-2>=a) ++ans; } print(ans); return 0; }
题意:给你m+1个数让你判断所给的数的二进制形式与第m+1个数不想同的位数是否小于等于k,累计答案
能再水点吗。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005; int a[N], my, ans; int main() { int n=getint(), m=getint(), k=getint(); for1(i, 1, m) read(a[i]); read(my); for1(i, 1, m) { int tot=0; for3(j, n-1, 0) { if(((1<<j)&a[i])!=((1<<j)&my)) ++tot; } if(tot<=k) ++ans; } print(ans); return 0; }
题意:给你n个数,让你分成k块不相交的大小为m的连续的块,然后求所有可行方案的最大值
设d[i, j]表示前i个数分成j块的最大值
d[i, j]=max{d[k, j-1]}+sum[i-m, i],1<=k<=i-m
答案是max{d[i, k]}
这里是n^3的,我们考虑优化横n^2
设mx[i, j]表示max{d[k, j]} 1<=k<=j
然后转移变成
d[i, j]=mx[i-m, j-1]+sum[i-m, i]
而mx的转移是
mx[i, j]=max{mx[i-1, j], d[i, j]}
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; typedef long long ll; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%I64d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const ll getint() { ll r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const ll max(const ll &a, const ll &b) { return a>b?a:b; } inline const ll min(const ll &a, const ll &b) { return a<b?a:b; } const int N=5005; int n, m, k; ll sum[N], d[N], ans, mx[N][N], f[N], a[N]; int main() { read(n); read(m); read(k); for1(i, 1, n) read(a[i]), sum[i]=sum[i-1]+a[i]; for1(i, m, n) d[i]=sum[i]-sum[i-m]; for1(i, m, n) { for3(j, k, 1) { f[j]=mx[i-m][j-1]+d[i]; mx[i][j]=max(mx[i-1][j], f[j]); } ans=max(ans, f[k]); } print(ans); return 0; }
字符串题,,,,
题意:给你一个文本串,不分大小,里边有n个单词,然后给你m个转换,求转换后的文本串的最小“r”的数量并且文本的长度最短
QAQ
有环啊啊 啊。。还要开long long
所以dfs之前我们要缩点。。。
然后缩点后我们dfs维护最小,然后输出答案。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <map> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=500005; string tp; map<string, int> mp; char rdin[N]; int n, m, tot, d[N], ln[N], cnt, ihead[N], vis[N], mn[N], essay[N], FF[N], LL[N], scc, tm, top, q[N], len[N], belongs[N], U[N], V[N]; struct ED { int next, to; }e[N*4]; void add(int u, int v) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; } int cal(char *s) { int len=strlen(s), ret=0; rep(j, len) if(s[j]=='r') ++ret; return ret; } int readin() { scanf("%s", rdin); int len=strlen(rdin); rep(j, len) if(rdin[j]>='A'&&rdin[j]<='Z') rdin[j]=rdin[j]-'A'+'a'; tp=rdin; if(mp[tp]) return mp[tp]; mp[tp]=++tot; ln[tot]=strlen(rdin); d[tot]=cal(rdin); return tot; } void dfs(int u) { vis[u]=1; int v; for(int i=ihead[u]; i; i=e[i].next) { if(!vis[v=e[i].to]) dfs(v); if(mn[v]<mn[u] || (mn[v]==mn[u] && len[v]<len[u])) { mn[u]=mn[v]; len[u]=len[v]; } } } void tarjan(int u) { LL[u]=FF[u]=++tm; vis[u]=1; q[++top]=u; int v; for(int i=ihead[u]; i; i=e[i].next) { v=e[i].to; if(!FF[v]) tarjan(v), LL[u]=min(LL[u], LL[v]); else if(vis[v]) LL[u]=min(LL[u], FF[v]); } if(FF[u]==LL[u]) { ++scc; int x; do { x=q[top--]; vis[x]=0; belongs[x]=scc; if(d[x]<mn[scc] || (d[x]==mn[scc]&&ln[x]<len[scc])) mn[scc]=d[x], len[scc]=ln[x]; } while(x!=u); } } void rebuild() { CC(vis, 0); CC(ihead, 0); cnt=0; for1(i, 1, m) if(belongs[U[i]]!=belongs[V[i]]) add(belongs[U[i]], belongs[V[i]]); } int main() { read(n); for1(i, 1, n) essay[i]=readin(); read(m); for1(i, 1, m) { int pos1=readin(), pos2=readin(); add(pos1, pos2); U[i]=pos1, V[i]=pos2; } CC(mn, 0x3f); CC(len, 0x3f); for1(i, 1, tot) if(!FF[i]) tarjan(i); rebuild(); for1(i, 1, scc) dfs(i); long long ans1=0, ans2=0; for1(i, 1, n) { int pos=belongs[essay[i]]; ans1+=mn[pos]; ans2+=len[pos]; } printf("%I64d %I64d\n", ans1, ans2); return 0; }
E.没看题。。