标签:its font 大学 ice lse bool 次数 other lin
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<ctime> 5 #include<cstdlib> 6 #include<algorithm> 7 #include<cmath> 8 #include<string> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 #include<utility> 14 using namespace std; 15 int read(){ 16 int xx=0,ff=1;char ch=getchar(); 17 while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)ff=-1;ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){xx=xx*10+ch-‘0‘;ch=getchar();} 19 return xx*ff; 20 } 21 int N,a; 22 long long ans,tim; 23 struct obj{ 24 int v; 25 bool type; 26 bool friend operator<(const obj&A,const obj&B) 27 {return A.v>B.v||(A.v==B.v&&A.type<B.type);} 28 }; 29 inline obj make_obj(int x,bool y){ 30 obj temp; 31 temp.v=x,temp.type=y; 32 return temp; 33 } 34 priority_queue<obj>pq; 35 int main(){ 36 //freopen("in","r",stdin); 37 for(int T=read();T;T--){ 38 N=read(); 39 ans=tim=0; 40 for(int i=1;i<=N;i++){ 41 a=read(); 42 if(pq.empty()) 43 pq.push(make_obj(a,0)); 44 else{ 45 obj tp=pq.top(); 46 if(tp.v>=a) 47 pq.push(make_obj(a,0)); 48 else{ 49 pq.pop(); 50 ans+=a-tp.v; 51 if(!tp.type){ 52 tim+=2; 53 pq.push(make_obj(a,1)); 54 } 55 else{ 56 pq.push(make_obj(tp.v,0)); 57 pq.push(make_obj(a,1)); 58 } 59 } 60 } 61 } 62 while(!pq.empty()) 63 pq.pop(); 64 printf("%I64d %I64d\n",ans,tim); 65 } 66 return 0; 67 }
04
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Special Judge
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<ctime> 5 #include<cstdlib> 6 #include<algorithm> 7 #include<cmath> 8 #include<string> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 #include<utility> 14 using namespace std; 15 int read(){ 16 int xx=0,ff=1;char ch=getchar(); 17 while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)ff=-1;ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){xx=xx*10+ch-‘0‘;ch=getchar();} 19 return xx*ff; 20 } 21 int n,a; 22 int main(){ 23 //freopen("in","r",stdin); 24 for(int T=read();T;T--){ 25 n=read(),a=read(); 26 if(n>2||n==0) 27 puts("-1 -1"); 28 else if(n==1) 29 printf("%d %d\n",1,1+a); 30 else //n==2 31 { 32 int k=a/2; 33 if(a&1) 34 printf("%d %d\n",2*k*k+2*k,2*k*k+2*k+1); 35 else 36 printf("%d %d\n",k*k-1,k*k+1); 37 } 38 } 39 return 0; 40 }
09
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
题意:
首先给出一个含有n个节点的树,边权为距离。
对于1-n的某一种排列p1,p2,p3……pn,贡献为dis(p1,p2)+dis(p2,p3)+dis(p3,p4)+……+dis(pn-1,pn)
求所有排列的贡献和
题解:观察整个式子,每个dis(i , j)出现的次数相同,均为(n-1)!
答案就是(n-1)!*sigma( dis(i , j) )
sigma( dis(i , j) )怎么求?
先求出1到所有点的距离和,然后在树上dfs转移动
例如有一条边a-->b,权值为v,设以b为根的子树大小为w
已知a到所有点的距离和da,就可以算出b到所有点的距离和db=da+(n-2*w)*v
(考虑这条边的贡献)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<ctime> 5 #include<cstdlib> 6 #include<algorithm> 7 #include<cmath> 8 #include<string> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 #include<utility> 14 using namespace std; 15 int read(){ 16 int xx=0,ff=1;char ch=getchar(); 17 while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)ff=-1;ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){xx=xx*10+ch-‘0‘;ch=getchar();} 19 return xx*ff; 20 } 21 const int MOD=int(1e9+7); 22 const int maxn=100010; 23 int N,sum[maxn],val[maxn],mul; 24 int lin[maxn],len; 25 struct edge{ 26 int y,next,v; 27 }e[maxn*2]; 28 inline void insert(int xx,int yy,int vv){ 29 e[++len].next=lin[xx]; 30 lin[xx]=len; 31 e[len].y=yy; 32 e[len].v=vv; 33 } 34 void dfs1(int x,int fa,int d){ 35 (val[1]+=d)%=MOD; 36 sum[x]=1; 37 for(int i=lin[x];i;i=e[i].next) 38 if(e[i].y!=fa){ 39 dfs1(e[i].y,x,(d+e[i].v)%MOD); 40 sum[x]+=sum[e[i].y]; 41 } 42 } 43 void dfs2(int x,int fa){ 44 for(int i=lin[x];i;i=e[i].next) 45 if(e[i].y!=fa){ 46 val[e[i].y]=(val[x]+1LL*e[i].v*(N-2*sum[e[i].y])%MOD)%MOD; 47 if(val[e[i].y]<0) 48 val[e[i].y]+=MOD; 49 dfs2(e[i].y,x); 50 } 51 } 52 int main(){ 53 //freopen("in","r",stdin); 54 while(scanf("%d",&N)!=EOF){ 55 memset(lin,0,sizeof(lin));len=0; 56 memset(val,0,sizeof(val)); 57 for(int i=1;i<N;i++){ 58 int t1=read(),t2=read(),t=read(); 59 insert(t1,t2,t); 60 insert(t2,t1,t); 61 } 62 mul=1; 63 for(int i=1;i<N;i++) 64 mul=1LL*mul*i%MOD; 65 dfs1(1,0,0); 66 dfs2(1,0); 67 int ans=0; 68 for(int i=1;i<=N;i++) 69 (ans+=val[i])%=MOD; 70 ans=1LL*ans*mul%MOD; 71 printf("%d\n",ans); 72 } 73 return 0; 74 }
2018中国大学生程序设计竞赛 - 网络选拔赛 01 04 09
标签:its font 大学 ice lse bool 次数 other lin
原文地址:https://www.cnblogs.com/lzhAFO/p/9534778.html