2018CCPC网络赛

A - Buy and Resell

 HDU - 6438 

The Power Cube is used as a stash of Exotic Power. There are nn cities numbered 1,2,,n1,2,…,n where allowed to trade it. The trading price of the Power Cube in the ii-th city is aiai 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 ii-th city and choose exactly one of the following three options on the ii-th day: 

1. spend aiai dollars to buy a Power Cube 
2. resell a Power Cube and get aiai 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 nn 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. 

InputThere are multiple test cases. The first line of input contains a positive integer TT (T250T≤250), indicating the number of test cases. For each test case: 
The first line has an integer nn. (1n1051≤n≤105) 
The second line has nn integers a1,a2,,ana1,a2,…,an where aiai means the trading price (buy or sell) of the Power Cube in the ii-th city. (1ai1091≤ai≤109) 
It is guaranteed that the sum of all nn is no more than 5×1055×105. 
OutputFor 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

利用pq贪心去看看那天选择了

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define val first
#define pos second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-9,e=exp(1),PI=acos(-1.);
struct T
{
    int v,cnt;
    friend bool operator <(T a,T b)
    {
        if(a.v==b.v) return a.cnt<b.cnt;
        return a.v>b.v;
    }
};
priority_queue<T>Q;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T,n;
    cin>>T;
    while(T--)
    {
        cin>>n;
        ll ans=0,cnt=0;
        for(int i=1,x,y; i<=n; i++)
        {
            cin>>x;
            if(!Q.empty())
            {
                y=Q.top().v;
                if(y<x)Q.pop(),ans+=x-y,Q.push({x,1});
            }
            Q.push({x,0});
        }
        while(!Q.empty())cnt+=Q.top().cnt,Q.pop();
        cout<<ans<<" "<<cnt*2<<"\n";
    }
    return 0;
}

CF上的神仙代码

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define val first
#define pos second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-9,e=exp(1),PI=acos(-1.);
multiset<pll> pq,se1,se2;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T,n;
    cin>>T;
    while(T--)
    {
        pq.clear(),se1.clear(),se2.clear();
        cin>>n;
        for(int i=1,x,y; i<=n; i++)
        {
            cin>>x;
            pq.insert({x, i});
            y=(*pq.begin()).val;
            if(y<x)
            {
                if(se2.empty()||(*se2.begin()).val>y)
                {
                    se1.insert(*pq.begin());
                    pq.erase(pq.begin());
                    se2.insert({x,i});
                    pq.erase(pq.find({x,i}));
                }
                else
                {
                    pq.insert(*se2.begin());
                    se2.erase(se2.begin());
                    se2.insert({x,i});
                    pq.erase(pq.find({x,i}));
                }
            }
            else if(!se2.empty()&&(*se2.begin()).val < x)
            {
                pq.insert(*se2.begin());
                se2.erase(se2.begin());
                se2.insert({x,i});
                pq.erase(pq.find({x,i}));
            }
        }
        ll a=0;
        for(auto X:se1)a-=X.val;
        for(auto X:se2) a+=X.val;
        cout<<a<<" "<<se1.size()+se2.size()<<endl;
    }
    return 0;
}

C - Dream

 HDU - 6440 

Freshmen frequently make an error in computing the power of a sum of real numbers, which usually origins from an incorrect equation (m+n)p=mp+np(m+n)p=mp+np, where m,n,pm,n,p are real numbers. Let's call it ``Beginner's Dream''. 

For instance, (1+4)2=52=25(1+4)2=52=25, but 12+42=172512+42=17≠25. Moreover, 9+16−−−−−√=25−−√=59+16=25=5, which does not equal 3+4=73+4=7. 

Fortunately, in some cases when pp is a prime, the identity 
(m+n)p=mp+np(m+n)p=mp+np

holds true for every pair of non-negative integers m,nm,n which are less than pp, with appropriate definitions of addition and multiplication. 

You are required to redefine the rules of addition and multiplication so as to make the beginner's dream realized. 

Specifically, you need to create your custom addition and multiplication, so that when making calculation with your rules the equation (m+n)p=mp+np(m+n)p=mp+np is a valid identity for all non-negative integers m,nm,n less than pp. Power is defined as 
ap={1,ap1a,p=0p>0ap={1,p=0ap−1⋅a,p>0


Obviously there exists an extremely simple solution that makes all operation just produce zero. So an extra constraint should be satisfied that there exists an integer q(0<q<p)q(0<q<p) to make the set {qk|0<k<p,kZ}{qk|0<k<p,k∈Z} equal to {k|0<k<p,kZ}{k|0<k<p,k∈Z}. What's more, the set of non-negative integers less than pp ought to be closed under the operation of your definitions. 

Hint

Hint for sample input and output: 
From the table we get 0+1=10+1=1, and thus (0+1)2=12=11=1(0+1)2=12=1⋅1=1. On the other hand, 02=00=002=0⋅0=0, 12=11=112=1⋅1=1, 02+12=0+1=102+12=0+1=1. 
They are the same. 

InputThe first line of the input contains an positive integer T(T30)T(T≤30)indicating the number of test cases. 

For every case, there is only one line contains an integer p(p<210)p(p<210), described in the problem description above. pp is guranteed to be a prime. 
OutputFor each test case, you should print 2p2p lines of pp integers. 

The jj-th(1jp1≤j≤p) integer of ii-th(1ip1≤i≤p) line denotes the value of (i1)+(j1)(i−1)+(j−1). The jj-th(1jp1≤j≤p) integer of (p+i)(p+i)-th(1ip1≤i≤p) line denotes the value of (i1)(j1)(i−1)⋅(j−1). 
Sample Input

1
2

Sample Output

0 1
1 0
0 0
0 1

构造一个群满足上述条件

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
#define pq priority_queue
const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-9,e=exp(1),PI=acos(-1.);
int a[N];
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        int p;
        cin>>p;
        for(int i=0; i<p; i++)
        {
            for(int j=0; j<p; j++)
                cout<<" "<<(i+j)%p;
            cout<<"\n";
        }
        for(int i=0; i<p; i++)
        {
            for(int j=0; j<p; j++)
                cout<<" "<<(i*j)%p;
            cout<<"\n";
        }
    }
    return 0;
}

E - GuGu Convolution

 HDU - 6442 

As a newbie, XianYu is now learning generating function! 
Given a series {a}=(a0,a1,a2,){a}=(a0,a1,a2,⋯), we can easily define its exponential generating function as g{a}(x)=i=0aii!xig{a}(x)=∑i=0∞aii!xi. 
Now we define a series {uc}=(c0,c1,c2,){uc}=(c0,c1,c2,⋯) and let ecec represents the ucuc with 00 filled in all its even items. Formally, {ec}=(0,c1,0,c3,0,c5,){ec}=(0,c1,0,c3,0,c5,⋯). 

'Do you know convolution?' 
'GU GU.' GuGu utters. 
'Well, let me show you. 
Given two generating function g{a}g{a} and g{b}g{b}, the convolution can be represented as G(x)=(g{a}g{b})(x)=n=0(i+j=naibj)xnG(x)=(g{a}∗g{b})(x)=∑n=0∞(∑i+j=naibj)xn. 
It is quite easy, right?' 
'GU GU.' GuGu utters. 
'Ok. Now you have to find the coefficient of xnxn of the convolution G(x)=(g{uA}g{eB√})G(x)=(g{uA}∗g{eB}), given nn, AA and BB. 
Let GnGn representes that coefficient, you should tell me n!Gnn!Gn. 
You may know the severity of unsolving this problem.' 

As GuGu is not that kind of good for it, it turns to you for help. 
'GU GU!' GuGu thanks. 

Hint

First Sample: 1!(100!1–√11!+111!00!)=11–√1!(100!111!+111!00!)=11 
Second Sample: 2!(52300!02!+52311!12−−√11!+52322!00!)=20923–√2!(52300!02!+52311!1211!+52322!00!)=20923 
P.S.: 104612−−√104612 is equal to the answer. However, 1212 has a factor 4=224=22 so it can't be output directly. 

InputThere is an integer TT in the first line, representing the number of cases. 
Then followed TT lines, and each line contains four integers A,B,n,pA,B,n,p. The meaning of A,B,nA,B,n is described above, and that of ppwill be described in Output session. 
1T1051≤T≤105 
1A,B1061≤A,B≤106 
1n10181≤n≤1018 
1p1091≤p≤109
OutputLet i=1qaibi−−√∑i=1qaibi represents the answer, with bibj,gcd(bi,bj)=1,1i<jqbi≠bj,gcd(bi,bj)=1,1≤i<j≤q, and none of bibi's factors is square number. 
Print TT lines only. Each line comes with a number qq and followed qqpairs of integers aiai bibi, with bibi in increasing order. Since aiai may be large, please print ai%pai%p instead. All integers in the same line should be seperated by exactly one space. 
You may find that each answer is unique. 
Sample Input

3
1 1 1 7
523 12 2 2100
1 1 1000000000000000000 998244353

Sample Output

1 1 1
1 2092 3
1 121099884 1

公式变换,然后作差

为了避免求逆出现问题,*2又/2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>v;
bool f[1000];
int la(int n)
{
    if(n==1) return 1;
    int res=1;
    for(auto i:v)
    {
        if(n%i==0)
        {
            int num=0;
            while(n%i==0)
            {
                n/=i;
                if(num&1) res*=i;
                num++;
            }
        }
        if(i*i>n) break;
    }
    return res;
}

ll qmi(ll p,ll q,ll n,ll mod,ll B)
{
    ll a=1,b=1,t1,t2;
    while(n)
    {
        if (n&1)
        {
            t1=(a*p+b*q%mod*B)%mod;
            t2=(a*q+b*p)%mod;
            a=t1;
            b=t2;
        }
        t1=(p*p+q*q%mod*B)%mod;
        t2=(p*q*2)%mod;
        p=t1;
        q=t2;
        n>>=1;
    }
    return b;
}
int main()
{
    for(int i=2; i<1000; i++)
        if(!f[i])
        {
            v.push_back(i);
            for (int j=i+i; j<1000; j+=i) f[j]=1;
        }
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    ll n,A,B,p,ans;
    cin>>T;
    while(T--)
    {
        cin>>A>>B>>n>>p;
        p*=2;
        ans=(qmi(A,1,n,p,B)-qmi(A,-1,n,p,B)+p)%p;
        ans/=2,p/=2;
        int k=la(B);
        cout<<1<<" "<<ans*k%p<<" "<<B/k/k<<"\n";
    }
}

G - Neko's loop

 HDU - 6444 

Neko has a loop of size nn. 
The loop has a happy value aiai on the ith(0in1)i−th(0≤i≤n−1) grid. 
Neko likes to jump on the loop.She can start at anywhere. If she stands at ithi−th grid, she will get aiai happy value, and she can spend one unit energy to go to ((i+k)modn)th((i+k)modn)−th grid. If she has already visited this grid, she can get happy value again. Neko can choose jump to next grid if she has energy or end at anywhere. 
Neko has mm unit energies and she wants to achieve at least ss happy value. 
How much happy value does she need at least before she jumps so that she can get at least ss happy value? Please note that the happy value which neko has is a non-negative number initially, but it can become negative number when jumping.

InputThe first line contains only one integer T(T50)T(T≤50), which indicates the number of test cases. 
For each test case, the first line contains four integers n,s,m,k(1n104,1s1018,1m109,1kn)n,s,m,k(1≤n≤104,1≤s≤1018,1≤m≤109,1≤k≤n). 
The next line contains nn integers, the ithi−th integer is ai1(109ai1109)ai−1(−109≤ai−1≤109) 
OutputFor each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.Sample Input

2
3 10 5 2
3 2 1
5 20 6 3
2 3 2 1 5

Sample Output

Case #1: 0
Case #2: 2

求一个环的最大值,有两种情况,用滑窗维护下吧

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
ll a[MAX];
ll sum[MAX];
int v[MAX];
vector<ll>p;
deque<int>que;
ll cal(ll m)
{
    int len=p.size();
    for(int i=0; i<len; i++)p.push_back(p[i]);          //扩大2倍
    for(int i=0; i<p.size(); i++)sum[i]=(i==0?p[i]:sum[i-1]+p[i]);
    int y=m%len;
    ll ans=0;
    while(!que.empty())que.pop_back();
    for(int i=0,R=0; i<len; i++)        //先走m/len圈
    {
        while(!que.empty()&&que.back()<i)que.pop_back();
        while(R<=i+y-1)
        {
            while(!que.empty()&&sum[que.front()]<=sum[R])que.pop_front();
            que.push_front(R);
            R++;
        }
        int x=que.back();
        ll res=max(0ll,sum[x]-(i==0?0:sum[i-1]));
        ans=max(ans,res+max(0ll,m/len*sum[len-1]));
    }
    if(m>=len)              //先找到最大值所需步数x,再走(m-x)/len圈
    {
        while(!que.empty())que.pop_back();
        for(int i=0,R=0; i<len; i++)
        {
            while(!que.empty()&&que.back()<i)que.pop_back();
            while(R<=i+len-1)
            {
                while(!que.empty()&&sum[que.front()]<=sum[R])que.pop_front();
                que.push_front(R);
                R++;
            }
            int x=que.back();
            ll res=max(0ll,sum[x]-(i==0?0:sum[i-1]));
            ans=max(ans,res+max(0ll,(m-(x-i+1))/len*sum[len-1]));
        }
    }
    return ans;
}
int main()
{
    int T,cas=1;
    cin>>T;
    while(T--)
    {
        ll n,s,m,k;
        scanf("%lld%lld%lld%lld",&n,&s,&m,&k);
        for(int i=0; i<n; i++)scanf("%lld",&a[i]);
        for(int i=0; i<n; i++)v[i]=0;
        ll ans=s;
        for(int i=0; i<n; i++)
        {
            if(v[i])continue;
            p.clear();
            int j=i;
            while(v[j]==0)      //找出循环节里的数并计算答案
            {
                v[j]=1;
                p.push_back(a[j]);
                j=(j+k)%n;
            }
            ans=min(ans,max(s-cal(m),0ll));
        }
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}

I - Tree and Permutation

 HDU - 6446 

There are NN vertices connected by N1N−1 edges, each edge has its own length. 
The set { 1,2,3,,N1,2,3,…,N } contains a total of N!N! unique permutations, let’s say the ii-th permutation is PiPi and Pi,jPi,j is its jj-th number. 
For the ii-th permutation, it can be a traverse sequence of the tree with NN vertices, which means we can go from the Pi,1Pi,1-th vertex to the Pi,2Pi,2-th vertex by the shortest path, then go to the Pi,3Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,NPi,N-th vertex, let’s define the total distance of this route as D(Pi)D(Pi) , so please calculate the sum of D(Pi)D(Pi) for all N!N!permutations.

InputThere are 10 test cases at most. 
The first line of each test case contains one integer NN ( 1N1051≤N≤105 ) . 
For the next N1N−1 lines, each line contains three integer XX, YY and LL, which means there is an edge between XX-th vertex and YY-th of length LL ( 1X,YN,1L1091≤X,Y≤N,1≤L≤109 ) .
OutputFor each test case, print the answer module 109+7109+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!个排列里,两两相邻是(n-1)!

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define v first
#define w second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
#define pq priority_queue
const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-9,e=exp(1),PI=acos(-1.);
vector<pair<int,int> > tree[N];
ll dp[N],sum[N];//统计每个点子树所包含点的个数
int n;
void dfs(int cur,int fa)
{
    sum[cur] = 1;
    for(int i = 0; i<tree[cur].size(); i++)
    {
        int son=tree[cur][i].v;
        ll len=tree[cur][i].w;
        if(fa==son)continue;
        dfs(son,cur);
        sum[cur]+=sum[son];
        dp[cur]+=dp[son]+sum[son]*(n-sum[son])%MD*len%MD;
        dp[cur]%=MD;
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        ll ans=2;
        memset(dp,0,sizeof(dp)),memset(sum,0,sizeof(sum));
        for(int i = 1,u,v,w; i<n; i++)
        {
            ans=ans*i%MD;
            scanf("%d%d%d",&u,&v,&w);
            tree[u].push_back({v,w});
            tree[v].push_back({u,w});
        }
        dfs(1,0);
        for(int i=1; i<=n; i++)tree[i].clear();
        printf("%d\n",ans*dp[1]%MD);
    }
    return 0;
}

J - YJJ's Salesman

 HDU - 6447 

YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. 
One day, he is going to travel from city A to southeastern city B. Let us assume that A is (0,0)(0,0) on the rectangle map and B (109,109)(109,109). YJJ is so busy so he never turn back or go twice the same way, he will only move to east, south or southeast, which means, if YJJ is at (x,y)(x,y) now (0x109,0y109)(0≤x≤109,0≤y≤109), he will only forward to (x+1,y)(x+1,y), (x,y+1)(x,y+1) or (x+1,y+1)(x+1,y+1). 
On the rectangle map from (0,0)(0,0) to (109,109)(109,109), there are several villages scattering on the map. Villagers will do business deals with salesmen from northwestern, but not northern or western. In mathematical language, this means when there is a village kk on (xk,yk)(xk,yk) (1xk109,1yk109)(1≤xk≤109,1≤yk≤109), only the one who was from (xk1,yk1)(xk−1,yk−1) to (xk,yk)(xk,yk) will be able to earn vkvk dollars.(YJJ may get different number of dollars from different village.) 
YJJ has no time to plan the path, can you help him to find maximum of dollars YJJ can get.

InputThe first line of the input contains an integer T(1T10)(1≤T≤10),which is the number of test cases. 

In each case, the first line of the input contains an integer N(1N105)(1≤N≤105).The following NN lines, the kk-th line contains 3 integers, xk,yk,vkxk,yk,vk (0vk103)(0≤vk≤103), which indicate that there is a village on (xk,yk)(xk,yk) and he can get vkvk dollars in that village. 
The positions of each village is distinct.
OutputThe maximum of dollars YJJ can get.Sample Input

1
3
1 1 1
1 2 2
3 3 1

Sample Output

3

离散化就可以用数据结构去维护这个dp了

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct T
{
    int x,y,v;
} t[N];
bool cmp(T a, T b)
{
    return a.x<b.x;
}
int a[N],c[N];
int tot;
void update(int i, int val)
{
    for(; i<=tot; i+=i&(-i))c[i]=max(c[i], val);
}
int query(int i)
{
    int res=0;
    for(; i>0; i-=i&(-i))res = max(res, c[i]);
    return res;
}
int dp[N];
int main()
{
    int T,n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for(int i=0; i<n; i++)scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].v);
        tot=0,memset(c,0,sizeof c);
        for(int i=0; i<n; i++)a[tot++]=t[i].y;
        sort(a,a+tot);
        tot=unique(a,a+tot)-a;
        for(int i=0; i<n; i++)t[i].y=lower_bound(a,a+tot,t[i].y)-a+1;
        sort(t,t+n,cmp);
        for(int i=0; i<n; i++) dp[i]=t[i].v;
        int pos=0,ans=0;
        for(int i=0; i<n; i++)
        {
            while(pos<i&&t[pos].x<t[i].x)update(t[pos].y,dp[pos]),pos++;
            dp[i]=query(t[i].y-1)+t[i].v,ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

posted @ 2018-09-06 09:03  暴力都不会的蒟蒻  阅读(...)  评论(...编辑  收藏