D.装饰
列方程
#include <iostream> #include<cstdio> #include<algorithm> typedef long long ll; using namespace std; ll a[5]; int main() { int t;scanf("%d",&t); while(t--){ scanf("%lld%lld%lld",&a[0],&a[1],&a[2]); sort(a,a+3); ll tmp=2*(a[0]+a[1])-a[2]; if(tmp<=0){ ll ans=a[0]+a[1]; printf("%lld ",ans); } else if(tmp%3==0){ ll ans=tmp/3+(a[2]-tmp/3)/2; printf("%lld ",ans); } else{ ll ans1=tmp/3+(a[2]-tmp/3)/2; ll ans2=(tmp/3+1)+(a[0]+a[1]-2*(tmp/3+1)); printf("%lld ",max(ans1,ans2)); } } return 0; }
二分
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #define inf 0x3f3f3f3f3f3f3f3f typedef long long ll; using namespace std; ll n,m,c; ll t[100005]; ll bus_time[100005]; ll bus_num[100005]; bool check(ll x){ for(ll i=1;i<=m;i++){ bus_num[i]=0,bus_time[i]=inf; } ll now=1; for(ll i=1;i<=n;i++){ if(bus_num[now]==c){ if(now+1>m) return 0; else now++,bus_num[now]=1,bus_time[now]=t[i]; } else{ if(t[i]-bus_time[now]<=x) bus_num[now]++; else{ if(now+1>m) return 0; else now++,bus_num[now]=1,bus_time[now]=t[i]; } } } return 1; } int main() { scanf("%lld%lld%lld",&n,&m,&c); for(ll i=1;i<=n;i++) scanf("%lld",&t[i]); sort(t+1,t+1+n); ll l=0,r=t[n]; while(l<=r){ ll mid=(l+r)/2; //printf("mid=%lld %d ",mid,check(mid)); if(check(mid)) r=mid-1; else l=mid+1; } printf("%lld ",l); return 0; }
模拟+优先队列
#include <iostream> #include<cstdio> #include<queue> #include<algorithm> typedef long long ll; using namespace std; struct Node{ ll s,t,id; bool operator<(const Node& b) const{ return id>b.id; } }cow[100005]; priority_queue<Node> q; bool cmp(Node a,Node b){ if(a.s!=b.s) return a.s<b.s; else return a.id<b.id; } int main() { ll n;scanf("%lld",&n); for(ll i=1;i<=n;i++){ scanf("%lld%lld",&cow[i].s,&cow[i].t); cow[i].id=i; } sort(cow+1,cow+1+n,cmp); ll p=1,last=0,ans=0; while(p<=n){ while(cow[p].s<=last) { q.push(cow[p]); p++; if(p>n) break; } if(q.empty()){ last=cow[p].s+cow[p].t; p++; } else{ Node top=q.top(); //printf("last=%d top.s=%d top.t=%d ",last,top.s,top.t); ans=max(ans,last-top.s); last=last+top.t; q.pop(); } //printf("last=%d ans=%d ",last,ans); } printf("%lld ",ans); return 0; }
dfs求连通块+模拟
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int dire[4][2]={0,-1,-1,0,0,1,1,0}; int n,k; const int m=10; char Map[110][15],t[110]; int vis[110][15]; inline bool in(int x,int y) {return x>=1&&x<=n&&y>=1&&y<=m;} int dfs(int x,int y){ //printf("(%d,%d) %c ",x,y,Map[x][y]); vis[x][y]=1; int ret=1; for(int i=0;i<4;i++){ int nx=x+dire[i][0]; int ny=y+dire[i][1]; if(in(nx,ny)&&Map[nx][ny]==Map[x][y]&&!vis[nx][ny]) ret+=dfs(nx,ny); } return ret; } void del(int x,int y){ char c=Map[x][y]; Map[x][y]='0'; for(int i=0;i<4;i++){ int nx=x+dire[i][0]; int ny=y+dire[i][1]; if(in(nx,ny)&&Map[nx][ny]==c) del(nx,ny); } } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ scanf("%s",Map[i]+1); } while(1){ bool flag=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ memset(vis,0,sizeof(vis)); if(Map[i][j]!='0'&&dfs(i,j)>=k){ flag=1,del(i,j); } } } if(!flag) break; for(int j=1;j<=m;j++){ int cnt=0; for(int i=1;i<=n;i++) if(Map[i][j]!='0') t[++cnt]=Map[i][j]; for(int i=n-cnt+1,p=1;i<=n;i++,p++) Map[i][j]=t[p]; for(int i=1;i<=n-cnt;i++) Map[i][j]='0'; } //for(int i=1;i<=n;i++) puts(Map[i]+1); } for(int i=1;i<=n;i++) puts(Map[i]+1); return 0; }
E.排队
bitset求所有组合情况
#include <iostream> #include<cstdio> #include<bitset> #include<vector> #include<algorithm> using namespace std; int pre[1005],vis[1005]; vector<int> a; bitset<1005> bs; int main() { int n,p;scanf("%d%d",&n,&p); for(int i=1;i<=n;i++){ scanf("%d",&pre[i]); if(pre[i]) vis[pre[i]]=1; } for(int i=1;i<=n;i++){ if(vis[i]) continue; int x=i,t=0,flag=0; while(x){ t++; if(x==p) flag=1; x=pre[x]; } if(!flag) a.push_back(t); } bs[0]=1; for(auto t:a){ bs|=(bs<<t); } int x=p,k=0; while(x){ k++; x=pre[x]; } for(int i=0;i<=n;i++) if(bs[i]) printf("%d ",i+k); return 0; }