2018中国大学生程序设计竞赛 - 网络选拔赛 01 04 09
Posted lzhafo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018中国大学生程序设计竞赛 - 网络选拔赛 01 04 09相关的知识,希望对你有一定的参考价值。
01
Buy and Resell
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai dollars if he has at least one Power Cube
3. do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.
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 ",ans,tim); 65 } 66 return 0; 67 }
04
Find Integer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Special Judge
give you two integers n,a,you are required to find 2 integers b,c such that a^n+b^n=c^n.
next T lines contains two integers n,a;(0≤n≤1000,000,000,3≤a≤40000)
else print two integers -1 -1 instead.
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 ",1,1+a); 30 else //n==2 31 { 32 int k=a/2; 33 if(a&1) 34 printf("%d %d ",2*k*k+2*k,2*k*k+2*k+1); 35 else 36 printf("%d %d ",k*k-1,k*k+1); 37 } 38 } 39 return 0; 40 }
09
Tree and Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
The set { 1,2,3,…,N } contains a total of N! unique permutations, let’s say the i-th permutation is Pi and Pi,j is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the Pi,1-th vertex to the Pi,2-th vertex by the shortest path, then go to the Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,N-th vertex, let’s define the total distance of this route as D(Pi) , so please calculate the sum of D(Pi) for all N! permutations.
The first line of each test case contains one integer N ( 1≤N≤105 ) .
For the next N?1 lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X,Y≤N,1≤L≤109 ) .
题意:
首先给出一个含有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 ",ans); 72 } 73 return 0; 74 }
以上是关于2018中国大学生程序设计竞赛 - 网络选拔赛 01 04 09的主要内容,如果未能解决你的问题,请参考以下文章
HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛
2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation dfs+树上两点距离和
2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心
2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 优先队列维护最小堆+贪心
HDU6447 YJJ's Salesman 2018中国大学生程序设计竞赛 - 网络选拔赛1010 离散化+线段树+DP