T1: 赛车 Bzoj3190
画个s-t图像,写个水平可见直线,注意只维护x>=0的部分。
#include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #include<algorithm> #include<stack> using namespace std; const int maxn = 10005; int n; struct line{ int a,b;int id; }L[maxn]; bool cmp(line x,line y){return x.a<y.a || (x.a==y.a && x.b>y.b);} bool ans[maxn]; int Ans[maxn]; stack<double>sta1;stack<line>sta2; double cross(line x,line y){return (y.b-x.b)*1.0/(x.a-y.a);} int main(){ freopen("race.in","r",stdin); freopen("race.out","w",stdout); scanf("%d",&n); int maxb=-100000; for(int i=1;i<=n;i++){scanf("%d",&L[i].b);L[i].id=i;maxb=max(maxb,L[i].b);} for(int i=1;i<=n;i++){scanf("%d",&L[i].a);if(L[i].b==maxb)ans[i]=1;} sort(L+1,L+1+n,cmp); sta1.push(0);sta2.push(L[1]); for(int i=2;i<=n;i++){ if(L[i].a==L[i-1].a)continue; while(!sta2.empty() && cross(L[i],sta2.top())<sta1.top())sta1.pop(),sta2.pop(); if(!sta2.empty())sta1.push(cross(L[i],sta2.top()));else sta1.push(0); sta2.push(L[i]); } int cnt=0; while(!sta2.empty()){ans[sta2.top().id]=1;sta2.pop();} for(int i=1;i<=n;i++)if(L[i].a==L[i-1].a && L[i].b==L[i-1].b)ans[L[i].id]=ans[L[i-1].id]; int t=0; for(int i=1;i<=n;i++)if(ans[i])Ans[++t]=i; printf("%d\n",t); for(int i=1;i<t;i++)printf("%d ",Ans[i]);printf("%d\n",Ans[t]); } /* 4 1 1 0 0 15 16 10 20 */
T2:卡牌游戏Bzoj3191
是谁不重要,重要的是和庄家的距离,用f[i][j]表示还剩i个人时,从庄家开始数第j个人胜率,枚举所有状态,f[i][j]由f[i-1][*]转移即可
#include<bits/stdc++.h> using namespace std; #define MADOKA main const int maxn = 55; int a[maxn]; int n,m; double f[maxn][maxn]; int MADOKA(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++)scanf("%d",&a[i]); f[1][1]=1; for(int i=2;i<=n;i++)for(int j=1;j<=i;j++)for(int k=1;k<=m;k++){ int tmp=a[k]%i; if(tmp==0)tmp=i;if(tmp==j)continue; if(tmp>j)tmp=i+j-tmp;else tmp=j-tmp; f[i][j]+=f[i-1][tmp]*1.0/m; } for(int i=1;i<n;i++)printf("%.2lf%% ",f[n][i]*100.0); printf("%.2lf%%\n",f[n][n]*100.0); }
T3:删除物品Bzoj3192
如图:
#include<bits/stdc++.h> using namespace std; const int maxn = 100005; #define LL long long struct node{ int k,p; }a[maxn]; int n1,n2,n; LL ans=0; bool cmp(node a,node b){ return a.k>b.k; } LL c[maxn]; #define lowbit(x) (x&-x) void add(int pos,int d){ for(int i=pos;i<=n;i+=lowbit(i))c[i]+=d; } LL query(int pos){ LL ret=0;for(int i=pos;i>0;i-=lowbit(i))ret+=c[i];return ret; } LL query(int pos1,int pos2){ int ret; if(pos1>pos2)ret=0; else ret=query(pos2)-query(pos1-1); return ret; } int main(){ freopen("remove.in","r",stdin); freopen("remove.out","w",stdout); scanf("%d%d",&n1,&n2); n=n1+n2; for(int i=1;i<=n1;i++){ scanf("%d",&a[i].k);a[i].p=n1-i+1; } for(int i=n1+1;i<=n;i++){ scanf("%d",&a[i].k);a[i].p=i; } sort(a+1,a+1+n,cmp); LL P=n1; for(int i=1;i<=n;i++){ int x=a[i].p; if(x < P)ans+=(LL)P-x-query(x+1,P),P=x; else if(x>P) ans+=(LL)x-1-P-query(P+1,x-1),P=x-1; add(x,1); } cout<<ans<<endl; }