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.
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.
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).
0 0 0 1
1 0 1 1
0 0 1 1
0 1 1 0
0 0 1 2
-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;
}
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:
- The maximum beauty difference of flowers that Pashmak can give to Parmida.
- 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.
The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
2 1 2
1 1
3 1 4 5
4 1
5 3 1 2 3 1
2 4
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:
- choosing the first and the second flowers;
- choosing the first and the fifth flowers;
- choosing the fourth and the second flowers;
- 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;
}
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.
The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).
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.
3 2 2
1 1 2 1 2 1
3 2 1
-1
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;
}
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.
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).
Print a single integer — the answer to the problem.
7 1 2 1 1 2 2 1
8
3 1 1 1
1
5 1 2 3 4 5
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;
}
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.
The first line contains two integers n, m (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: ui, vi, wi (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.
Print a single integer — the answer to the problem.
3 3 1 2 1 2 3 1 3 1 1
1
3 3 1 2 1 2 3 2 3 1 3
3
6 7 1 2 1 3 2 5 2 4 2 2 5 2 2 6 9 5 4 3 4 3 4
6
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;
}