There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.
Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.
The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.
The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is equal to the number written on thei-th card.
Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.
It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.
6 1 5 7 4 4 3
1 3 6 2 4 5
4 10 10 10 10
1 2 3 4
In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.
In the second sample, all values ai are equal. Thus, any distribution is acceptable.
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
int p,v;
}num[110];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int main()
{
int n,a;
int l;
while(~scanf("%d",&n))
{
int l=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
num[i].p=i;
num[i].v=a;
}
sort(num+1,num+1+n,cmp);
for(int i=1;i<=n/2;i++)
printf("%d %d\n",num[i].p,num[n+1-i].p);
}
return 0;
}
Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.
The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.
You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which arenot under attack after Vasya puts it on the board.
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.
Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.
Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.
3 3 1 1 3 1 2 2
4 2 0
5 2 1 5 5 1
16 9
100000 1 300 400
9999800001
#include<string.h>
int visit[100010];
int vis[100010];
int main()
{
__int64 p,q,m,n,x,y;
while(~scanf("%I64d%I64d",&n,&m))
{
memset(visit,0,sizeof(visit));
memset(vis,0,sizeof(vis));
p=q=n;
while(m--)
{
scanf("%I64d%I64d",&x,&y);
if(p==0||q==0)
printf("0\n");
else
{
if(vis[x]&&!visit[y])
{
q--;
visit[y]=1;
}
else if(!vis[x]&&visit[y])
{
p--;
vis[x]=1;
}
else if(!vis[x]&&!visit[y])
{
q--;
p--;
vis[x]=1;
visit[y]=1;
}
printf("%I64d\n",q*p);
}
}
}
return 0;
}