NOIP前刷水行动
2016.11.15
BZOJ1009:DP+矩阵乘法+KMP
BZOJ1898:矩阵乘法
BZOJ4101:贪心,考虑我们往右边撞的时候,我们会向左边冲
,于是枚举答案点利用一个指针计算即可!
2016.11.14
OI队内测试
2016.11.13
BZOJ4512:乱搞
BZOJ4102:DP+bfs
BZOJ4395:bfs
BZOJ3889:双键值最短路
- BZOJ4512
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- int N,sx,sy,ans;
- char s[];
- bool vis[][],north[][],east[][];
- int main(){
- // freopen("data.in","r",stdin);
- // freopen("A.out","w",stdout);
- scanf("%d%s",&N,s);
- sx=sy=;
- for (int i=;i<N;i++){
- vis[sx][sy]=;
- char ch=s[i];
- if (ch=='N'){
- sy++;
- if (!north[sx][sy-]&&vis[sx][sy])ans++;
- north[sx][sy-]=;
- }
- if (ch=='S'){
- sy--;
- if (!north[sx][sy]&&vis[sx][sy])ans++;
- north[sx][sy]=;
- }
- if (ch=='E'){
- sx++;
- if (!east[sx-][sy]&&vis[sx][sy])ans++;
- east[sx-][sy]=;
- }
- if (ch=='W'){
- sx--;
- if (!east[sx][sy]&&vis[sx][sy])ans++;
- east[sx][sy]=;
- }
- }
- printf("%d\n",ans);
- }
- BZOJ4102
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define ll long long
- #define ld long double
- #define N 1005
- #define M N*(N+1)
- using namespace std;
- int n,p,tot;
- int pre[M],v[M],now[N],f[N],dis[N][N];
- bool vis[N];
- queue<int>q;
- struct data{
- int val,pos;
- }a[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b){++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;
- }
- bool cmp(data a,data b){return a.val<b.val;
- }
- void bfs(int u)
- {
- memset(vis,,sizeof(vis));
- q.push(u); dis[u][u]=; vis[u]=;
- while (!q.empty())
- {
- int x=q.front(); q.pop();
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (!vis[son])
- {
- dis[u][son]=dis[u][x]+;
- q.push(son); vis[son]=;
- }
- }
- }
- }
- int main()
- {
- n=read(); p=read();
- for (int i=; i<=n; i++)
- {
- int x=read(),d=read();
- a[i].val=x; a[i].pos=i;
- for (int j=; j<=d; j++) {int y=read(); ins(i,y); ins(y,i);}
- }
- for (int i=; i<=n; i++) bfs(i);
- sort(a+,a+n+,cmp);
- for (int i=; i<=n; i++) f[i]=a[i].val;
- for (int i=; i<=n; i++)
- for (int j=; j<=i-; j++)
- {
- if (a[i].val>a[j].val && dis[a[i].pos][a[j].pos])
- {
- f[i]=max(f[i],f[j]-dis[a[i].pos][a[j].pos]*p+a[i].val);
- }
- }
- int ans=;
- for (int i=; i<=n; i++) ans=max(ans,f[i]);
- printf("%d\n",ans);
- return ;
- }
- BZOJ4395
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define ll long long
- #define ld long double
- #define N 205
- #define M 40005
- using namespace std;
- int dx[]={,,,-},dy[]={,-,,};
- int pre[M],v[M],now[N*N],tot;
- int n,m,ans;
- queue<int>q;
- bool vis[N][N],lig[N][N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int encode(int a,int b){return (a-)*n+b;
- }
- void ins(int a,int b){
- ++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=m; i++)
- {
- int a=read(),b=read(),c=read(),d=read();
- ins(encode(a,b),encode(c,d));
- }
- int last=; ans=;
- for (;;)
- {
- q.push(encode(,));
- memset(vis,,sizeof(vis)); vis[][]=; lig[][]=;
- while (!q.empty())
- {
- int pp=q.front(),x,y; q.pop(); //last++;
- for (int p=now[pp]; p; p=pre[p])
- {
- int son=v[p]; x=(son-)/n+,y=(son-)%n+;
- if (!lig[x][y]) lig[x][y]=,ans++;
- }
- x=(pp-)/n+,y=(pp-)%n+;
- for (int i=; i<; i++)
- {
- int xx=x+dx[i],yy=y+dy[i];
- if (xx< || xx>n || yy< || yy>n || !lig[xx][yy] || vis[xx][yy]) continue;
- vis[xx][yy]=; q.push(encode(xx,yy));
- }
- }
- if (last==ans) break;
- last=ans;
- }
- printf("%d\n",ans);
- return ;
- }
- BZOJ3889
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define ll long long
- #define ld long double
- #define inf 100000000000000
- #define N 1005
- using namespace std;
- int s,t,m,n;
- ll a[N][N],b[N][N],dis[N],d[N];
- int c[N];
- bool vis[N];
- queue<int>q;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void spfa()
- {
- for (int i=; i<=m; i++) dis[i]=inf;
- dis[s]=d[s]=; vis[s]=; q.push(s);
- while (!q.empty())
- {
- int x=q.front(); q.pop(); vis[x]=;
- //cout<<" "<<x<<endl;
- for (int i=; i<=m; i++)
- {
- // cout<<i<<" "<<dis[i]<<" "<<dis[x]<<" "<<a[x][i]<<endl;
- if (dis[i]>dis[x]+a[x][i] || ((dis[i]==dis[x]+a[x][i]) && d[i]>d[x]+b[x][i]))
- {
- // cout<<" "<<x<<" "<<i<<" "<<a[x][i]+dis[x]<<endl;
- dis[i]=dis[x]+a[x][i]; d[i]=d[x]+b[x][i];
- if (!vis[i]) vis[i]=,q.push(i);
- }
- }
- //|| (dis[i]==dis[x]+a[x][i] && d[i]>d[x]+b[x][i])
- }
- }
- int main()
- {
- s=read(); t=read(); n=read();
- for (int i=; i<=; i++)
- for (int j=; j<=; j++) a[i][j]=inf;
- for (int i=; i<=n; i++)
- {
- int len=read(),cnt=read();
- for (int j=; j<=cnt; j++) c[j]=read(),m=max(m,c[j]);
- for (int j=; j<=cnt; j++)
- for (int k=j+; k<=cnt; k++)
- if (a[c[j]][c[k]]>len || ((a[c[j]][c[k]]==len) && (b[c[j]][c[k]]>(k-j)))) a[c[j]][c[k]]=len,b[c[j]][c[k]]=k-j;
- }
- spfa();
- if (dis[t]==inf) dis[t]=-,d[t]=-;
- cout<<dis[t]<<" "<<d[t]<<endl;
- return ;
- }
2016.11.12
BZOJ3887: spfa(正边一次+反边一次)
BZOJ3886:状态压缩dp+二分(f[i]表示看电影状态为i的最长连续时间)
BZOJ4098:DP优化(f[i][j][k][l]:(i+j)==(k+l) 优化为f[i][j][k]:(i+j)满足滚动数组
BZOJ3888:把时间求出来+线段树
- BZOJ3887
- #include<algorithm>
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define N 100005
- #define M 200005
- using namespace std;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int n,m;
- int a[N],b[N];
- int tot,pre[M],v[M],now[N];
- int bel[N],low[N],dfn[N],num[N],total,ind,top,z[N];
- int dist1[N],dist2[N];
- bool inq[N],vis[N];
- queue<int> q;
- void ins(int a,int b){++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;}
- void tarjan(int x)
- {
- low[x]=dfn[x]=++ind;
- ++top; z[top]=x; inq[x]=;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (!dfn[son]) tarjan(son),low[x]=min(low[x],low[son]);
- else if (inq[son]) low[x]=min(low[x],dfn[son]);
- }
- if (low[x]==dfn[x])
- {
- ++total; num[total]=; inq[x]=; bel[x]=total;
- while (z[top]!=x)
- {
- int son=z[top]; bel[son]=total; inq[son]=; num[total]++; top--;
- }
- top--;
- }
- }
- void build(int X)
- {
- memset(now,,sizeof(now)); tot=;
- for (int i=; i<=m; i++)
- {
- int x=a[i],y=b[i];
- if (bel[x]!=bel[y])
- {
- if (X==) ins(bel[x],bel[y]); else ins(bel[y],bel[x]);
- }
- }
- }
- void spfa1()
- {
- vis[bel[]]=; dist1[bel[]]=num[bel[]]; q.push(bel[]);
- while (!q.empty())
- {
- int x=q.front(); q.pop(); vis[x]=;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (dist1[son]<dist1[x]+num[son])
- {
- dist1[son]=dist1[x]+num[son];
- if (!vis[son]) q.push(son),vis[son]=;
- }
- }
- }
- }
- void spfa2()
- {
- vis[bel[]]=; dist2[bel[]]=num[bel[]]; q.push(bel[]);
- while (!q.empty())
- {
- int x=q.front(); q.pop(); vis[x]=;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (dist2[son]<dist2[x]+num[son])
- {
- dist2[son]=dist2[x]+num[son];
- if (!vis[son]) q.push(son),vis[son]=;
- }
- }
- }
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=m; i++) a[i]=read(),b[i]=read(),ins(a[i],b[i]);
- for (int i=; i<=n; i++) if (!dfn[i]) tarjan(i);
- build(); spfa1();
- build(); spfa2();
- int ans=;
- for (int i=; i<=m; i++) if (bel[a[i]]!=bel[b[i]] && dist1[bel[b[i]]] && dist2[bel[a[i]]])
- {
- ans=max(ans,dist1[bel[b[i]]]+dist2[bel[a[i]]]-num[bel[]]);
- }
- printf("%d\n",ans);
- return ;
- }
- BZOJ3886
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- using namespace std;
- int n,L;
- int num[],len[],c[][];
- int f[];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int find(int x,int s)
- {
- int l=,r=num[x],Ans=-;
- while (l<=r)
- {
- int mid=(l+r)>>;
- if (c[x][mid]<=s) Ans=mid,l=mid+; else r=mid-;
- }
- return Ans;
- }
- int main()
- {
- n=read(); L=read();
- for (int i=; i<=n; i++)
- {
- len[i]=read(); num[i]=read();
- for (int j=; j<=num[i]; j++) c[i][j]=read();
- }
- f[]=;
- int s=<<n,ans=0x7fffffff;
- for (int i=; i<s; i++) f[i]=-;
- for (int i=; i<s; i++)
- {
- if (f[i]==-) continue;
- int k=;
- if (f[i]>L)
- {
- for (int j=i; j; j>>=) if (j&) k++;
- ans=min(ans,k); continue;
- }
- for (int j=; j<=n; j++)
- {
- if (i&(<<(j-))) continue;
- int k=find(j,f[i]); if (k==-) continue;
- f[i|(<<(j-))]=max(f[i|(<<(j-))],c[j][k]+len[j]);
- }
- }
- if (ans==0x7fffffff) ans=-;
- printf("%d\n",ans);
- return ;
- }
- BZOJ4098
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #define N 505
- #define mod 1000000007
- #define add(a,b) (a=(a+b)%mod)
- using namespace std;
- int n,f[][N][N];
- char s[N][N];
- int main()
- {
- scanf("%d",&n);
- for (int i=; i<=n; i++) scanf("%s",s[i]+);
- if(s[][]!=s[n][n])
- {
- puts("");
- return ;
- }
- int now=,last=;
- f[now][][]=;
- for (int h=; h<n; h++)
- {
- now^=; last^=;
- memset(f[now],,sizeof(f[now]));
- for (int i=; i<=h; i++)
- {
- int j=(h-i),a=i+,b=j+;
- for (int k=; k<=h; k++)
- {
- int l=h-k,x=n-l,y=n-k;
- /* if (s[a+1][b]==s[x][y-1]) (f[now][i+1][k+1]+=f[last][i][k])%mod;
- if (s[a+1][b]==s[x-1][y]) (f[now][i+1][k]+=f[last][i][k])%mod;
- if (s[a][b+1]==s[x][y-1]) (f[now][i][k+1]+=f[last][i][k])%mod;
- if (s[a][b+1]==s[x-1][y]) (f[now][i][k]+=f[last][i][k])%mod; */
- if(s[a+][b]==s[x-][y])add(f[now][i+][ k ],f[last][i][k]);
- if(s[a+][b]==s[x][y-])add(f[now][i+][k+],f[last][i][k]);
- if(s[a][b+]==s[x-][y])add(f[now][ i ][ k ],f[last][i][k]);
- if(s[a][b+]==s[x][y-])add(f[now][ i ][k+],f[last][i][k]);
- }
- }
- }
- int ans=;
- for (int i=; i<n; i++) add(ans,f[last][i][i]);
- printf("%d\n",ans);
- return ;
- }
- BZOJ3888
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #define N 50005
- using namespace std;
- int n,tot;
- struct data{
- int h,l,r;
- }a[N];
- int b[*N],cov[N*];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- bool cmp2(data a,data b){return a.h<b.h;
- }
- int get(int x,int c)
- {
- return x>=?:-x*c;
- }
- int cover(int k,int l,int r,int x,int y)
- {
- if (cov[k]) return ;
- if (x<=l && r<=y)
- {
- cov[k]=; return ;
- }
- bool ret=;
- int mid=(l+r)>>;
- if (x<=mid) ret|=cover(k*,l,mid,x,y);
- if (mid<y) ret|=cover(k*+,mid+,r,x,y);
- cov[k]=cov[k*]&cov[k*+];
- return ret;
- }
- int find(int x)
- {
- int l=,r=tot,ans=;
- while (l<=r)
- {
- int mid=(l+r)>>;
- if (b[mid]<=x) ans=mid,l=mid+; else r=mid-;
- }
- return ans;
- }
- int main()
- {
- n=read();
- for (int i=; i<=n; i++)
- {
- int x=read(),y=read(),c=read();
- int l=get(x+,c),r=get(x,c);
- a[i].l=l,a[i].r=r,a[i].h=y;
- b[++tot]=l; b[++tot]=r;
- }
- sort(b+,b+tot+);
- for (int i=; i<=n; i++) a[i].l=find(a[i].l),a[i].r=find(a[i].r)-;
- sort(a+,a+n+,cmp2);
- int ans=;
- for (int i=; i<=n; i++) ans+=cover(,,tot,a[i].l,a[i].r);
- printf("%d\n",ans);
- return ;
- }
2016.11.11
BZOJ4396 :贪心
BZOJ4397 :前缀和
BZOJ3943 :最小生成树
BZOJ4576:DP:这道题还可以网上有很多种写法
BZOJ4582:DP:之前看错题想了好久最后发现题目意思 好水
BZOJ4412:贪心:这道题也不错,我们首先将每个数减1,如果存在j-->x的子段和大于0,
则说明一定有牛走出去,而我们可以知道在顺时针方向一定存在一个
点没有牛越过去,于是我们找到那个点将环转化为链即可,而这个点
一定是最大子段和的左节点,于是问题解决
2016.11.10
BZOJ1592 Usaco2008 Feb]Making the Grade 路面修整:离散+DP
BZOJ1051 HAOI 受欢迎的牛 :tarjan
BZOJ2442 修建草坪 :单调队列优化DP
BZOJ3890 Meeting time : 分层DP
BZOJ4390 Max Flow :树上差分
BZOJ4525 :二分答案判定
BZOJ4511 :DP
- BZOJ1592
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #define N 3005
- #define inf 0x7fffffff
- using namespace std;
- int n,m,ans,a[N],c[N],f[N][N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>='' && ch<='');
- return x*f;
- }
- void solve()
- {
- sort(c+,c+n+);
- m=unique(c+,c+n+)-(c+);
- for (int i=; i<=n; i++) f[i][]=inf/;
- for (int i=; i<=n; i++)
- {
- for (int j=; j<=m; j++)
- {
- f[i][j]=min(f[i-][j]+abs(a[i]-c[j]),f[i][j-]);
- }
- }
- ans=f[n][m];
- for (int i=; i<=n; i++) f[i][m+]=inf/;
- for (int i=; i<=n; i++)
- {
- for (int j=m; j>=; j--)
- {
- f[i][j]=min(f[i-][j]+abs(a[i]-c[j]),f[i][j+]);
- }
- }
- ans=min(ans,f[n][]);
- }
- int main()
- {
- n=read();
- for (int i=; i<=n; i++) c[i]=a[i]=read();
- solve();
- printf("%d\n",ans);
- return ;
- }
- BZOJ1051
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 10005
- #define M 50005
- using namespace std;
- int n,m,ans,u[M],V[M];
- int now[N],pre[M],v[M],tot;
- int ind,top,total,z[N],dfn[N],low[N],num[N],in[N],out[N],belong[N];
- bool vis[N],inq[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b){
- ++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;
- }
- void dfs(int x)
- {
- dfn[x]=low[x]=++ind;
- vis[x]=inq[x]=; z[++top]=x;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (!dfn[son]) {dfs(son); low[x]=min(low[x],low[son]);}
- else if (inq[son]) low[x]=min(low[x],dfn[son]);
- }
- if (low[x]==dfn[x])
- {
- ++total;
- belong[x]=total; inq[x]=; num[total]=;
- while (z[top]!=x)
- {
- int son=z[top];
- belong[son]=total; inq[son]=; num[total]++;
- top--;
- }
- top--;
- }
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=m; i++)
- {
- int x=read(),y=read();
- u[i]=x; V[i]=y;
- ins(x,y);
- }
- for (int i=; i<=n; i++) if (!dfn[i]) dfs(i);
- for (int i=; i<=m; i++) if (belong[u[i]]!=belong[V[i]])
- {
- // cout<<" "<<belong[u[i]]<<" "<<belong[V[i]]<<endl;
- ++out[belong[u[i]]]; ++in[belong[V[i]]];
- }
- // for (int i=1; i<=total; i++) cout<<num[i]<<endl;
- int sum=;
- for (int i=; i<=total; i++)
- {
- if (!out[i]) {ans=num[i]; sum++; if (sum>=) break;}
- }
- // cout<<ans<<endl;
- if (sum>) ans=; printf("%d\n",ans);
- return ;
- }
- BZOJ
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 100005
- using namespace std;
- int n,k,head,tail;
- ll ans,sum,f[N];
- int a[N],pos[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- n=read(); k=read();
- for (int i=; i<=n; i++) a[i]=read(),sum+=a[i];
- for (int i=; i<=n; i++)
- {
- while (head<=tail && pos[head]<i-k-) head++;
- f[i]=f[pos[head]]+a[i];
- while (head<=tail && f[pos[tail]]>=f[i]) tail--;
- ++tail; pos[tail]=i;
- }
- ans=;
- for (int i=n-k; i<=n; i++) ans=min(ans,f[i]);
- printf("%lld\n",sum-ans);
- return ;
- }
- BZOJ3890
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define N 105
- #define M (N*(N+1))
- #define ll long long
- using namespace std;
- int pre[M],v[M],val1[M],val2[M],now[N],n,m,tot;
- bool f[N][M],g[N][M];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b,int c,int d)
- {
- ++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b; val1[tot]=c; val2[tot]=d;
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=m; i++)
- {
- int u=read(),v=read(),a=read(),b=read();
- ins(u,v,a,b);
- }
- f[][]=g[][]=;
- for (int i=; i<=n-; i++)
- {
- for (int p=now[i]; p; p=pre[p])
- {
- int son=v[p];
- for (int j=val1[p]; j<=; j++) f[son][j]|=f[i][j-val1[p]];
- for (int j=val2[p]; j<=; j++) g[son][j]|=g[i][j-val2[p]];
- }
- }
- bool bo=false;
- for (int i=; i<=; i++)
- if (g[n][i] && f[n][i])
- {
- bo=true;
- printf("%d\n",i);
- break;
- }
- if (!bo) printf("IMPOSSIBLE\n");
- return ;
- }
- BZOJ4390
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define N 60005
- #define M N*2
- #define ll long long
- using namespace std;
- int pre[M],v[M],now[N],tot,n,k,ans;
- int f[N][],bin[],deep[N],val[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b){++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;
- }
- void dfs(int x)
- {
- for (int i=; i<= && bin[i]<=deep[x]; i++) f[x][i]=f[f[x][i-]][i-];
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p]; if (son==f[x][]) continue;
- deep[son]=deep[x]+; f[son][]=x;
- dfs(son);
- }
- }
- int lca(int x,int y)
- {
- if (deep[x]<deep[y]) swap(x,y);
- int t=deep[x]-deep[y];
- for (int i=; bin[i]<=t; i++)
- {
- if (t&bin[i]) x=f[x][i];
- }
- for (int i=; i>=; i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
- if (x==y) return x;
- return f[x][];
- }
- void dfs2(int x,int fa)
- {
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p]; if (son==fa) continue;
- dfs2(son,x); val[x]+=val[son];
- }
- ans=max(ans,val[x]);
- }
- int main()
- {
- n=read(); k=read();
- for (int i=; i<n; i++)
- {
- int x=read(),y=read();
- ins(x,y); ins(y,x);
- }
- bin[]=; for (int i=; i<=; i++) bin[i]=bin[i-]*;
- deep[]=; dfs();
- for (int i=; i<=k; i++)
- {
- int x=read(),y=read();
- int t=lca(x,y);
- val[x]++; val[y]++; val[t]--; val[f[t][]]--;
- }
- dfs2(,);
- printf("%d\n",ans);
- return ;
- }
- BZOJ4525
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define inf 0x7ffffff
- using namespace std;
- int n,k,ans;
- int a[];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- bool pd(int mid)
- {
- int l=;
- for (int i=; i<=k; i++)
- {
- int pos=a[l+];
- while (l+<=n && ((a[l+]-pos)<=*mid)) l++;
- }
- if (l>=n) return true;
- return false;
- }
- int main()
- {
- n=read(); k=read();
- for (int i=; i<=n; i++) a[i]=read();
- sort(a+,a+n+);
- int l=,r=inf-;
- while (l<=r)
- {
- int mid=(l+r)>>;
- if (pd(mid)) ans=mid,r=mid-; else l=mid+;
- }
- printf("%d\n",ans);
- return ;
- }
- BZOJ4511
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 50005
- using namespace std;
- int ans,n,a[N],f[N][];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- n=read();
- for (int i=; i<=n; i++) a[i]=read()%;
- memset(f,-,sizeof(f));
- f[][]=; for (int i=; i<=n; i++) f[i][a[i]]=;
- for (int i=; i<=n; i++)
- for (int j=; j<=; j++)
- if (f[i-][j]!=-) f[i][(j+a[i])%]=max(f[i][(j+a[i])%],f[i-][j]+);
- for (int i=; i<=n; i++) ans=max(ans,f[i][]);
- printf("%d\n",ans);
- return ;
- }
代码集合
2016.11.9
BZOJ1711: 最大流
BZOJ1642: 排序+DP
- BZOJ1711
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #include<queue>
- #define inf 0x7fffffff
- #define ll long long
- #define N 505
- #define M 50005
- using namespace std;
- int n,F,D,head,tail,list[N<<],tot=,S,T,ans=;
- int pre[M],v[M],cap[M],now[N],deep[N];
- queue<int>q;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b,int c){
- ++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b; cap[tot]=c;
- ++tot; pre[tot]=now[b]; now[b]=tot; v[tot]=a; cap[tot]=;
- }
- void init_build()
- {
- n=read(); F=read(); D=read();
- S=; T=n*+F+D+;
- for (int i=; i<=F; i++) ins(S,i,);
- for (int i=F+n*+; i<=F+n*+D; i++) ins(i,T,);
- for (int i=; i<=n; i++)
- {
- int A=read(),B=read();
- for (int j=; j<=A; j++) {int x=read(); ins(x,F+i,);}
- for (int j=; j<=B; j++) {int x=read(); ins(F+n+i,F+n*+x,);}
- }
- for (int i=;i<=n;i++) ins(F+i,F+i+n,);
- }
- bool bfs()
- {
- for (int i=S;i<=T;i++) deep[i]=-;
- deep[S]=; head=,tail=,list[]=S;
- while (head<tail)
- {
- int x=list[++head];
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (cap[p] && deep[son]==-)
- {
- deep[son]=deep[x]+;
- if (son==T) return ;
- list[++tail]=son;
- }
- }
- }
- return ;
- }
- int find(int x,int tmpflow)
- {
- int temp,re;
- if (x==T) return tmpflow;
- re=temp=;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (cap[p] && deep[son]==deep[x]+)
- {
- temp=find(son,min(tmpflow,cap[p]));
- cap[p]-=temp;
- cap[p^]+=temp;
- re+=temp;
- tmpflow-=temp;
- if (tmpflow==) break;
- }
- }
- deep[x]=-;
- return re;
- }
- void dinic()
- {
- ans=;
- while (bfs())
- {
- ans+=find(S,inf);
- }
- }
- int main()
- {
- tot=;
- init_build();
- dinic();
- printf("%d\n",ans);
- return ;
- }
- BZOJ
- #include<iostream>
- #include<cstring>
- #include<cmath>
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- int n,m,r,ans=;
- int read(){
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- struct data{
- int l,r,val;
- }a[];
- int f[];
- bool cmp(data a,data b)
- {
- return a.l==b.l?a.r<b.r:a.l<b.l;
- }
- int main()
- {
- n=read(); m=read()+; r=read();
- a[].l=a[].r=-; a[].val=;
- for (int i=; i<=m; i++)
- {
- a[i].l=read(),a[i].r=read()-,a[i].val=read();
- }
- sort(a+,a+m+,cmp);
- for (int i=; i<=m; i++)
- {
- for (int j=; j<=i-; j++)
- {
- if (a[j].r+r<a[i].l)
- {
- f[i]=max(f[i],f[j]+a[i].val);
- ans=max(ans,f[i]);
- }
- }
- }
- printf("%d\n",ans);
- return ;
- }
代码集合
2016.11.8
BZOJ1708:背包
BZOJ1690:01分数规划+spfa+二分
BZOJ1669:LIS问题+单调栈
- BZOJ
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- using namespace std;
- int n,m;
- ll f[];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- n=read(); m=read(); f[]=;
- for (int i=; i<=n; i++)
- {
- int x=read();
- for (int i=x; i<=m; i++) f[i]+=f[i-x];
- }
- printf("%lld\n",f[m]);
- return ;
- }
- BZOJ1690
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 1005
- #define M 5005
- using namespace std;
- int n,m,tot;
- int a[N],now[N],pre[M],v[M];
- double ans,mid,val[M],value[N],dis[N];
- bool vis[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- void ins(int a,int b,int c){++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b; val[tot]=c;
- }
- bool spfa(int x)
- {
- vis[x]=;
- for (int p=now[x]; p; p=pre[p])
- {
- int son=v[p];
- if (dis[son]<dis[x]+value[son]-mid*val[p])
- {
- if (!vis[son])
- {
- dis[son]=dis[x]+value[son]-mid*val[p];
- if (spfa(son)) return ;
- }
- else return ;
- }
- }
- return vis[x]=;
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=n; i++) value[i]=read();
- for (int i=; i<=m; i++)
- {
- int a=read(),b=read(),c=read(); ins(a,b,c);
- }
- double l=,r=,eps=1e-;
- while (r-l>eps)
- {
- memset(dis,,sizeof(dis));
- memset(vis,,sizeof(vis));
- mid=(l+r)/2.0; dis[]=0.0;
- if (spfa()) ans=mid,l=mid; else r=mid;
- }
- printf("%0.2lf\n",ans);
- return ;
- }
- BZOJ1669
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define N 5005
- #define ll long long
- using namespace std;
- int n,tot;
- int b[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- int n=read(),tot=;
- for (int i=; i<=n; i++)
- {
- int x=read();
- if (x>b[tot])
- {
- tot++; b[tot]=x;
- }
- else
- {
- int l=tot;
- while (l>= && b[l]>=x) l--;
- //cout<<" "<<l<<endl;
- b[l+]=x;
- }
- }
- printf("%d\n",tot);
- return ;
- }
代码集合
2016.11.7
BZOJ3298:打表找规律
BZOJ3296:并查集
BZOJ1599:模拟
BZOJ1232:最小生成树
- BZOJ3298
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 1000005
- using namespace std;
- int n,m,f[N];
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- n=read()+; m=read()+;
- f[]=; int t=;
- for (int i=; i<=m; i++)
- {
- if (f[i]) continue;
- t++; f[i]=i+t; if (i+t<=m) f[i+t]=i;
- }
- int T=read();
- while (T--)
- {
- int x=read()+,y=read()+;
- if (f[x]==y) printf("Farmer John\n"); else printf("Bessie\n");
- }
- return ;
- }
- BZOJ3296
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 40005
- using namespace std;
- int fa[N],id[N],n,m,ans;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);
- }
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=n; i++) fa[i]=i;
- for (int i=; i<=n; i++)
- {
- int k=read();
- for (int j=; j<=k; j++){int x=read(); if (!id[x]) id[x]=i; else {fa[find(id[x])]=find(i); id[x]=i;}}
- }
- for (int i=; i<=n; i++) if (find(i)==i) ans++;
- printf("%d\n",ans-);
- return ;
- }
- BZOJ1599
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- using namespace std;
- int sum[],ans;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- int main()
- {
- int a=read(),b=read(),c=read();
- for (int i=; i<=a; i++)
- for (int j=; j<=b; j++)
- for (int k=; k<=c; k++) sum[i+j+k]++;
- sum[]=; ans=; for (int i=; i<=a+b+c; i++) if (sum[i]>sum[ans]) ans=i;
- printf("%d\n",ans);
- return ;
- }
- BZOJ1232
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<cstdio>
- #define ll long long
- #define N 100005
- using namespace std;
- int n,m;
- struct data{
- int u,v,val;
- }a[N];
- int c[N],Min=0x7fffffff,ans=,fa[N],cnt=;
- int read()
- {
- int x=,f=; char ch;
- while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
- while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
- return x*f;
- }
- bool cmp(data a,data b){return a.val<b.val;
- }
- int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
- int main()
- {
- n=read(); m=read();
- for (int i=; i<=n; i++) fa[i]=i;
- for (int i=; i<=n; i++) c[i]=read();
- for (int i=; i<=m; i++) a[i].u=read(),a[i].v=read(),a[i].val=read()*+c[a[i].u]+c[a[i].v];
- sort(a+,a+m+,cmp);
- for (int i=; i<=m; i++)
- {
- int u=a[i].u,v=a[i].v,val=a[i].val;
- int fu=find(u),fv=find(v);
- if (fv!=fu)
- {
- fa[fv]=fu; Min=min(Min,min(c[u],c[v]));
- ans+=val; cnt++; if (cnt==n-) break;
- }
- }
- printf("%d\n",ans+Min);
- return ;
- }
代码集合
NOIP前刷水行动的更多相关文章
- NOIP前刷题记录
因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...
- NOIP前的水题记录
CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- Noip前的大抱佛脚----Noip真题复习
Noip前的大抱佛脚----Noip真题复习 Tags: Noip前的大抱佛脚 Noip2010 题目不难,但是三个半小时的话要写四道题还是需要码力,不过按照现在的实力应该不出意外可以AK的. 机器翻 ...
- NOIP前的一些计划
一些想法 距离NOIP2018只剩下一个星期的时间了,通过这几天在长郡的考试,渐渐感觉还有好多东西自己还不够熟练,也有些东西到现在还不会,现将NOIP前的一些计划列在这里,希望能在考前把他们全部完成吧 ...
- Noip前的大抱佛脚----文章索引
Noip前的大抱佛脚----赛前任务 Noip前的大抱佛脚----考场配置 Noip前的大抱佛脚----数论 Noip前的大抱佛脚----图论 Noip前的大抱佛脚----动态规划 Noip前的大抱佛 ...
- Noip前的大抱佛脚----字符串
目录 字符串 经验 用FFT求解字符串匹配问题 两(多)串DP时状态合并 最长公共子序列转LIS 位运算最大值 挂链哈希 哈希处理回文串 树哈希 字符串模板库 KMP 最小循环表示 Mancher A ...
- Noip前的大抱佛脚----一些思路
目录 一些思路 序列 函数问题 网格图 删除和询问 乘法问题 顺序问题 最值问题 研究成果 数论分块套数论分块的复杂度 一些思路 Tags:Noip前的大抱佛脚 序列 线段树(当然还要有主席树啊!) ...
- Noip前的大抱佛脚----数论
目录 数论 知识点 Exgcd 逆元 gcd 欧拉函数\(\varphi(x)\) CRT&EXCRT BSGS&EXBSGS FFT/NTT/MTT/FWT 组合公式 斯特林数 卡塔 ...
随机推荐
- 转:Selenium中的几种等待方式,需特别注意implicitlyWait的用法
最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法. ...
- HDU 5240 Exam
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest 2015ACM-ICPC上海大都会赛 签到题 #include< ...
- sphinx分域搜索
http://stackoverflow.com/questions/2526407/complex-query-with-sphinx 比如要实现和如下sql代码相同的功能: SELECT * FR ...
- Entity Framework 6新功能Logging/Store Procedure
摘要 在Entity Framework6中有两个新的功能,DB Loggin和Stored Procedure的映射 Entity Framework 6已经从Beta版本来到了RC1版本,我们可以 ...
- Android5.0 Gallery2上编译Gallery模块出错
Android5.0 Gallery2上编译Gallery模块出错 时间:2015-05-05 19:32:57 阅读:105 评论:0 收藏:0 [点我收藏+ ...
- 草,又学了个新命令,nc传文件。
nc -l 5222 > aa nc 192.168.0.48 5222 < a http://www.linuxso.com/command/nc.html
- UILabel 解析及自适应
CGFloat width1=[(NSString *)ob1 sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMa ...
- Struts2--ModelDriven接收参数
1. JSP文件调用格式: <a href="user/user!add?name=a&age=8">添加用户</a> 2. struts.xml文 ...
- C#读取word文件
第一步:添加对在项目引用里添加上对Microsoft Word 11.0 object library的引用.右击--引用---在com标签下添加.
- Protocol buffers--python 实践(一) 简介以及安装与使用
由于最近对grpc产生了浓厚的兴趣,但是那是一整套东西,看了一下用到的东西不少,所以抽丝剥茧先写写这几天调研的protocol buffer -python的收获. 简介: 以下引用自官方首页文档: ...