1. 程式人生 > >2018中國大學生程序設計競賽 - 網絡選拔賽 01 04 09

2018中國大學生程序設計競賽 - 網絡選拔賽 01 04 09

b- 轉移 positive hid set fort row ase tween

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


Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,,n where allowed to trade it. The trading price of the Power Cube in the i
-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:

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.

Input There are multiple test cases. The first line of input contains a positive integer T (T250), indicating the number of test cases. For each test case:
The first line has an integer n. (1n105)
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. (1ai109)
It is guaranteed that the sum of all n is no more than 5×105.

Output For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.

Sample Input 3 4 1 2 10 9 5 9 5 9 10 5 2 2 1

Sample Output 16 4 5 2 0 0 Hint In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16 In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5 In the third case, he will do nothing and earn nothing. profit = 0 題意: 有n個城市編號1-n,每個城市的方塊有個價格。 商人從按照編號從小到大順序走過這些城市,到達某個城市的時候可以選擇買入或者賣出一個方塊(什麽都不做也行)。 問商人的最大盈利和最大盈利前提下最少的交易次數。 解析: 從小到大遍歷,用一個小根堆存儲可以買入的方塊價格。 當到達城市的方塊價格小於等於堆頂時直接入堆。 大於的話買入堆頂,然後賣出。 不過這樣是有問題的,可以用一些技巧讓商人能夠“後悔”。 堆中物品分為兩種(一種是一般的物品type=0,另一種是後悔的等價物type=1) 設當前城市價格為 p 當賣出的是價值為 q 的一般物品時,向堆中加入一個價值為 p 的後悔等價物。 當賣出的是價值為 q 的後悔等價物時,向堆中加入一個價值為 p 的後悔等價物和一個價值為 q 的普通物品。 經過這樣的操作,可以發現,如果賣掉後悔等價物,就相當於後悔了在某個城市的賣出交易。 例如現在是三個城市,價格a<b<c。 第一步:買入a,堆中元素:a 第二步:賣出a,ans+=b-a,堆中元素:等價物b。 第三步:賣出等價物b,ans+=c-b,堆中元素:b,等價物c 詳情可以看代碼 技術分享圖片
 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 }
View Code

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


Problem Description people in USSS love math very much, and there is a famous math problem .

give you two integers n,a,you are required to find 2 integers b,c such that a^n+b^n=c^n.

Input one line contains one integer T;(1T1000000)

next T lines contains two integers n,a;(0n1000,000,000,3a40000)

Output print two integers b,c if b,c exits;(1b,c1000,000,000);

else print two integers -1 -1 instead.

Sample Input 1 2 3

Sample Output 4 5

這道題首先要用到費馬大定理 當整數n >2時,關於x, y, z的方程 x^n + y^n = z^n 沒有正整數解。 那麽就只需要討論n=0,1,2了 n=0無解 n=1隨便構造一組即可 n=2需要找一組含有a的勾股數 https://baike.baidu.com/item/%E5%8B%BE%E8%82%A1%E6%95%B0/2674064 技術分享圖片
 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 }
View Code

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


Problem Description There are N vertices connected by N?1 edges, each edge has its own length.
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.

Input There are 10 test cases at most.
The first line of each test case contains one integer N ( 1N105 ) .
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 ( 1X,YN,1L109 ) .

Output For each test case, print the answer module 109+7 in one line.

Sample Input 3 1 2 1 2 3 1 3 1 2 1 1 3 2

Sample Output 16 24

題意:

首先給出一個含有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 }
View Code

2018中國大學生程序設計競賽 - 網絡選拔賽 01 04 09