Codeforces round #261 (Div. 2)

Source: Internet
Author: User

A. pashmak and garden

The coordinates of the two vertices are known. If the other two vertices can be broken, the special judge is output ). If these two vertices are not the two vertices that constitute a square,

Output-1.


Water question, 1a, not to mention.


#include<cstdio>#include<iostream>#include<cstring>#include<cmath>using namespace std;int main(){    int x1,x2,y1,y2,flag,x3,y3,x4,y4,l;    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)    {        int t1 = fabs(x2-x1);        int t2 = fabs(y2-y1);        flag = 0;        if(!t1)        {            l = t2;            x3 = x4 = x1+t2;            y3 = y1;            y4 = y2;        }        else if(!t2)        {            l = t1;            y3 = y4 = y1+t1;            x3 = x1;            x4 = x2;        }        else        {            if(t1!=t2) flag = 1;            else            {                l = t1;                x3 = x1;                y3 = y2;                x4 = x2;                y4 = y1;            }        }        if(flag) printf("-1\n");        else printf("%d %d %d %d\n",x3,y3,x4,y4);    }    return 0;}

B. pashmak and flowers

Question:

There are n flowers, each of which has a beauty value. What is the maximum difference between the beauty values that can be found? What are the solutions for choosing the two flowers with the greatest difference?

Output these two values.


You only need to count the maximum and minimum values, as well as the number of the maximum and minimum values. The number of solutions is cnt1 * cnt2.

Special Judgment: if the maximum difference value is 0, that is, when the maximum value is equal to the minimum value, the number of solutions is N * (n-1)/2.


#include<cstdio>#include<iostream>#include<cstring>#define maxn 200010#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;int f[maxn];int main(){    int n,maxv,minv;    ll cnt1,cnt2,ans;    scanf("%d",&n);    minv = INF,maxv = -INF;    for(int i=1;i<=n;i++)    {        scanf("%d",&f[i]);        if(f[i]>maxv) maxv = f[i];        if(f[i]<minv) minv = f[i];    }    cnt1 = cnt2 = 0;    for(int i=1;i<=n;i++)    {        if(f[i]==minv)            cnt1++;        else if(f[i]==maxv)            cnt2++;    }    if(maxv==minv) ans = (ll)n*(n-1)/2;    else ans = cnt1*cnt2;    printf("%d %I64d\n",maxv-minv,ans);    return 0;}


C. pashmak and buses

Question:

There are n students, K bus, d days. To arrange for N students to take D-day bus, so that no two students have the same d-day bus.


Algorithm:

First, every student has K kinds of ride plan (1-K car) every day, so there are K ^ d different ride schemes in D days.

If N> K ^ d, two students have the same itinerary. (Rejection principle) => in this case, output-1.

Because K is very large, long will also overflow if it is always multiplied by a square or a fast power, but if you only need to determine the size of K and N,

If n is at most 1000, set an INF value greater than 1000 and within the int range. If n is greater than INF, then break.


Second, the trouble is to output the daily car arrangement of N students.

I started to use the fully arranged next_permutation and thought of a solution. After a long change, I wrote 200 rows faster, but there was a problem. After reading other people's writing, I feel very subtle!

Because n students must have different solutions, at this time, the sequence of N % K + 1 and N/= K will be different to construct ans [I] [J] (j number of day I

Number of students on the bus)


#include<cstdio>#include<iostream>#include<cstring>#define INF 10000000#define maxn 1010using namespace std;int ans[maxn][maxn];int main(){    int n,k,d;    while(scanf("%d%d%d",&n,&k,&d)!=EOF)    {        int res = 1;        for(int i=1;i<=d;i++)        {            res = res*k;            if(res>INF)            {                res = INF;                break;            }        }        if(res<n)        {            printf("-1\n");            continue;        }        for(int i=1;i<=n;i++)        {            int t = i;            for(int j=1;j<=d;j++)            {                ans[j][i] = t%k+1;                t/=k;            }        }        for(int i=1;i<=d;i++)        {            for(int j=1;j<n;j++)                printf("%d ",ans[i][j]);            printf("%d\n",ans[i][n]);        }    }    return 0;}


D. pashmak and parmida's Problem

Question:

Define F (L, R, x) as the number of [L, R] intervals where a [I] = x.

Calculate the number of pair (I, j) that satisfies f (1, I, a [I])> F (J, n, a [J]) And I <j. 1 <= A [I] <= 10 ^ 9, 1? ≤?N? ≤? 106


Algorithm:

Calculate the number of equal numbers on the left of a [I] and the number of equal values on the right of a [J.

Because counting is required, the brute-force method is not time-consuming (two cycles), or the paid memory cannot be processed (A [I] has a maximum of 10 ^ 9, and the array cannot be opened ).

Use the map artifact to Count o (cost _ percent) o ha!


Then, the number of RI [I] is stored in a tree array.

From 1-N scans, the current Ri [I] value is updated each time-1. ensure that the subscript is j> I. in this case, j = I + 1 ..... I + n

Find the number of RI values in I + 1 that are smaller than LE [I.


#include<cstdio>#include<iostream>#include<cstring>#include<map>#define maxn 1000010using namespace std;typedef long long ll;map<int,int> s;int le[maxn],ri[maxn],c[maxn],n,a[maxn];int lowbit(int x){    return x&(-x);}int que(int pos){    int sum = 0;    while(pos>0)    {        sum+=c[pos];        pos -= lowbit(pos);    }    return sum;}void update(int pos,int v){    while(pos<=n)    {        c[pos]+=v;        pos += lowbit(pos);    }}int main(){    ll ans;    while(scanf("%d",&n)!=EOF)    {        s.clear();        memset(le,0,sizeof(le));        memset(ri,0,sizeof(ri));        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            s[a[i]]++;            le[i] = s[a[i]];        }        s.clear();        for(int j=n;j>=1;j--)        {            s[a[j]]++;            ri[j] = s[a[j]];        }        for(int i=n;i>=1;i--)            update(ri[i],1);        ans = 0;        for(int i=1;i<=n;i++)        {            update(ri[i],-1);            ans+=que(le[i]-1);        }        printf("%I64d\n",ans);    }    return 0;}

E. pashmak and Graph

A directed graph with n points and m edges. Find a path so that the edge weight on the path increases monotonically and the number of edges on the path is the largest.

Maximum number of output edges.


Algorithm:

First, sort by edge weight from small to large. If no equal edge exists, you can directly update DP [E [I]. to] = max (DP [E [I]. fr] + 1, DP [E [I]. to].

However, if there are equal edges, the number of equal edges is added to the number of edges in a path.

Therefore, we need to set an intermediate array f so that all edges with equal edge weights are first transferred from DP [E [I]. fr] And then updated together.


#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define maxn 300010using namespace std;int dp[maxn],n,m,f[maxn];struct node{    int fr,to,v;}e[maxn];bool cmp(node x,node y){    return x.v<y.v;}int main(){    int u,v,w;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=1;i<=m;i++)            scanf("%d%d%d",&e[i].fr,&e[i].to,&e[i].v);        sort(e+1,e+m+1,cmp);        memset(f,0,sizeof(f));        memset(dp,0,sizeof(dp));        for(int i=1;i<=m;i++)        {            int j = i;            while(j<=m && e[j].v == e[i].v) j++;            for(int k=i;k<j;k++)            {                int u = e[k].fr,v = e[k].to;                f[v] = max(f[v],dp[u]+1);            }            for(int k=i;k<j;k++)                dp[e[k].to] = f[e[k].to];            i = j-1;        }        int ans = 0;        for(int i=1;i<=n;i++)            ans = max(ans,dp[i]);        printf("%d\n",ans);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.