Codeforces Round #261 (Div. 2)

来源:互联网 发布:薄荷瘦身软件下载 编辑:程序博客网 时间:2024/05/22 03:52



Codeforces Round #261 (Div. 2)
A. Pashmak and Garden
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pashmak has fallen in love with an attractive girl called Parmida since one year ago...

Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.

Input

The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.

Output

If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.

Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).

Sample test(s)
input
0 0 0 1
output
1 0 1 1
input
0 0 1 1
output
0 1 1 0
input
0 0 1 2
output
-1
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){    int x1,x2,y1,y2;    cin>>x1>>y1>>x2>>y2;    if(x1==x2)    {        int a=abs(y1-y2);        cout<<x1+a<<" "<<y1<<" "<<x2+a<<" "<<y2<<endl;    }    else if(y1==y2)    {        int a=abs(x1-x2);        cout<<x1<<" "<<y1-a<<" "<<x2<<" "<<y2-a<<endl;    }    else    {        int dx=abs(x1-x2);        int dy=abs(y1-y2);        if(dx==dy)            cout<<x1<<" "<<y2<<" "<<x2<<" "<<y1<<endl;        else            cout<<-1<<endl;    }    return 0;}



B. Pashmak and Flowers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty numberbi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!

Your task is to write a program which calculates two things:

  1. The maximum beauty difference of flowers that Pashmak can give to Parmida.
  2. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
Input

The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1b2, ..., bn (1 ≤ bi ≤ 109).

Output

The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.

Sample test(s)
input
21 2
output
1 1
input
31 4 5
output
4 1
input
53 1 2 3 1
output
2 4
Note

In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:

  1. choosing the first and the second flowers;
  2. choosing the first and the fifth flowers;
  3. choosing the fourth and the second flowers;
  4. choosing the fourth and the fifth flowers.


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;int a[200200],n;int mx=-0x3f3f3f3f,mi=0x3f3f3f3f;int main(){    cin>>n;    for(int i=0;i<n;i++)    {        cin>>a[i];        mi=min(a[i],mi);        mx=max(a[i],mx);    }    long long int b=0,c=0;    for(int i=0;i<n;i++)    {        if(a[i]==mx)            b++;        if(a[i]==mi)            c++;    }    long long int ans=b*c;    if(mi==mx)        ans=1LL*n*(n-1)/2;    cout<<mx-mi<<" "<<ans<<endl;    return 0;}


C. Pashmak and Buses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has nstudents. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.

Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.

Input

The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).

Output

If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.

Sample test(s)
input
3 2 2
output
1 1 2 1 2 1 
input
3 2 1
output
-1
Note

Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day.

n换成k进制表示

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[1000][1000];int main(){    int n,k,d;    cin>>n>>k>>d;    for(int i=0;i<n;i++)    {        int t=i;        for(int j=0;j<d;j++)        {            a[i][j]=t%k;            t/=k;        }        if(t)        {            puts("-1");            return 0;        }    }    for(int i=0;i<d;i++)    {        for(int j=0;j<n;j++)        {            cout<<a[j][i]+1<<" ";        }        cout<<endl;    }    return 0;}


D. Pashmak and Parmida's problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
71 2 1 1 2 2 1
output
8
input
31 1 1
output
1
input
51 2 3 4 5
output
0
离散化+树状数组
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#define ERROR(x) printf(".......%d.........\n",x);using namespace std;const int maxn=1000100;int r[maxn],md[maxn],val[maxn],n,m;int zheng[maxn],fan[maxn],tree[maxn];map<int,int> mp;int lowbit(int x){    return x&(-x);}void add(int p,int v){    for(int i=p;i<=n;i+=lowbit(i))        tree[i]+=v;}int sum(int p){    int ret=0;    for(int i=p;i;i-=lowbit(i))        ret+=tree[i];    return ret;}bool cmpR(int a,int b){    return val[a]<val[b];}void Lisan(){    for(int i=0;i<=n;i++) r[i]=i;    sort(r+1,r+1+n,cmpR);    md[1]=val[r[1]];    val[r[1]]=m=1;    for(int i=2;i<=n;i++)    {        if(md[m]!=val[r[i]]) md[++m]=val[r[i]];        val[r[i]]=m;    }}int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        scanf("%d",val+i);    }    Lisan();    for(int i=n;i>=1;i--)    {        fan[i]=++mp[val[i]];        add(fan[i],1);    }    mp.clear();    long long int ans=0;    for(int i=1;i<=n;i++)    {        add(fan[i],-1);        zheng[i]=++mp[val[i]];        ans+=sum(zheng[i]-1);    }    cout<<ans<<endl;    return 0;}


E. Pashmak and Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.

You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

Help Pashmak, print the number of edges in the required path.

Input

The first line contains two integers nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: uiviwi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi.

It's guaranteed that the graph doesn't contain self-loops and multiple edges.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
3 31 2 12 3 13 1 1
output
1
input
3 31 2 12 3 23 1 3
output
3
input
6 71 2 13 2 52 4 22 5 22 6 95 4 34 3 4
output
6
Note

In the first sample the maximum trail can be any of this trails: .

In the second sample the maximum trail is .

In the third sample the maximum trail is .



排序后,看以v为终点的最大长度。。



#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn=300300;struct Edge{    int u,v,weight;}edge[maxn];bool cmp(Edge a,Edge b){    return a.weight<b.weight;}int n,m;int dp[maxn],f[maxn];int main(){    scanf("%d%d",&n,&m);    for(int i=0;i<m;i++)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        edge[i]=(Edge){a,b,c};    }    sort(edge,edge+m,cmp);    for(int i=0;i<m;i++)    {        int j=i;        for(;edge[j].weight==edge[i].weight;j++);        for(int k=i;k<j;k++)            dp[edge[k].v]=max(dp[edge[k].v],f[edge[k].u]+1);        for(int k=i;k<j;k++)            f[edge[k].v]=dp[edge[k].v];        i=j-1;    }    int ans=0;    for(int i=1;i<=n;i++)        ans=max(ans,f[i]);    cout<<ans<<endl;    return 0;}




3 0