zoukankan      html  css  js  c++  java
  • 2018中国大学生程序设计竞赛

    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
    ",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的勾股数
     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 }
    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 N1 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 N1 lines, each line contains three integer XY 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
    ",ans);
    72     }
    73     return 0;
    74 }
    View Code
     
     
  • 相关阅读:
    一往直前!贪心法
    Longest subarray of target sum
    动态规划-最长公共子序列/最长公共子串
    Short Encoding of Words
    Spiral Matrix
    Longest Repeating Character Replacement
    伤怀之美
    宁静是一种生产力
    POJ 2524 Ubiquitous Religions 【并查集】
    POJ 1611 The Suspects【并查集】
  • 原文地址:https://www.cnblogs.com/lzhAFO/p/9534778.html
Copyright © 2011-2022 走看看