模擬賽(11.4)
阿新 • • 發佈:2017-11-04
nss size scan weight none src oid long blog
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; int main() { freopen("block.in","r",stdin); freopen("block.out","w",stdout); int T; scanf("%d",&T); while(T--) {1long long n,ans=-1; scanf("%lld",&n); long long i=sqrt(n); for(long long j=i;j>=1;j--) { long long tmp=2*(i+(n/i)); if(n%i) tmp+=2; if(ans==-1)ans=tmp; else ans=min(ans,tmp); } printf("%lld\n",ans); } }
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; struct data { long long v; int x,y; bool operator < (const data &a)const { return v<a.v; } }e[220010]; int fa[20010]; int weight[20010]; int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);} int main() { freopen("tree.in","r",stdin); freopen("tree.out","w",stdout); int T; scanf("%d",&T); while(T--) { memset(e,0,sizeof(e)); int n; scanf("%d",&n); for(int i=1;i<=n;i++){fa[i]=i;weight[i]=1;} for(int i=1;i<n;i++) { scanf("%d%d",&e[i].x,&e[i].y); scanf("%lld",&e[i].v); } sort(e+1,e+n); long long ans=0; for(int i=1;i<n;i++) { int x=find(e[i].x),y=find(e[i].y),val=e[i].v; ans+=(long long)(weight[x]*weight[y])*(long long)(val+1)-1; fa[x]=y; weight[y]+=weight[x]; } printf("%lld\n",ans); } }
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> using namespace std; struct data { int a,b; bool operator <(const data &t) const { return a<t.a; } bool operator == (const data &t)const { return (a==t.a&&b==t.b); } scanf("%d",&nm[i]); }ans[200010]; int nm[200010]; int f[2][25][200010]; int mi[30]; int n; int mark[200010]; void work(int l,int r) { if(l>r) return; if(l+1==r) {ans[r].a=nm[l];ans[r].b=nm[r];ans[l].a=nm[l];ans[l].b=nm[r];return;} int tmp=r-l+1; int min1=n+1,min2=n+1; int p1=l,p2=l; int fg=l%2; for(int i=25;i>=0;i--) { if(mi[i]<tmp) { tmp-=mi[i]; min1=min(min1,f[fg][i][p1]); p1+=mi[i];ans[l].a=nm[l];ans[l].b=nm[r]; } } fg^=1; p1=mark[min1]; p2=p1+1; tmp=r-p2+1; for(int i=25;i>=0;i--) { if(mi[i]<=tmp) { tmp-=mi[i]; min2=min(min2,f[fg][i][p2]); p2+=mi[i]; } } p2=mark[min2]; work(l,p1-1);work(p1+1,p2-1);work(p2+1,r); int t1=l,t2=p1+1,t3=p2+1; data tm[200010];int cnt=0; memset(tm,0,sizeof(tm)); tm[cnt].a=min1,tm[cnt++].b=min2; tm[cnt].a=min1,tm[cnt++].b=min2; while(t1<=p1-1||t2<=p2-1||t3<=r) { data anss;anss.a=anss.b=n+1; if(t1<=p1-1) anss=min(anss,ans[t1]); if(t2<=p2-1) anss=min(anss,ans[t2]); if(t3<=r) anss=min(anss,ans[t3]); if(anss==ans[t1]) tm[cnt++]=ans[t1++]; else if(anss==ans[t2]) tm[cnt++]=ans[t2++]; else if(anss==ans[t3]) tm[cnt++]=ans[t3++]; } for(int i=0;i<cnt;i++) { ans[i+l]=tm[i]; } } int main() { freopen("ball.in","r",stdin); freopen("ball.out","w",stdout); mi[0]=1;for(int i=1;i<30;i++) mi[i]=mi[i-1]*2; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&nm[i]); mark[nm[i]]=i; f[i%2][0][i]=nm[i]; f[(i+1)%2][0][i]=n+1; } for(int i=1;i<=24;i++) { for(int j=1;j<=n;j++) { if(j+mi[i]-1<=n) { f[0][i][j]=min(f[0][i-1][j],f[0][i-1][j+mi[i-1]]); f[1][i][j]=min(f[1][i-1][j],f[1][i-1][j+mi[i-1]]); } } } work(1,n); for(int i=1;i<=n;i+=2) { printf("%d %d ",ans[i].a,ans[i].b); } }3
模擬賽(11.4)