1 second
256 megabytes
standard input
standard output
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 the i-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 to8.
In the second sample, all values ai are
equal. Thus, any distribution is acceptable.
这题没什么好说的,求出平均值然后两层循环遍历就可以了
#include<bits/stdc++.h>
using namespace std;
const int N=100+10;
int a[N],v[N];
int main()
{
int n,i;
while(~scanf("%d",&n))
{
memset(v,0,sizeof(v));
int sum=0;
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sum/=(n/2);
for(i=1; i<n; i++)
if(!v[i])
{
for(int j=i+1; j<=n; j++)
if(!v[j])
{
if(a[i]+a[j]==sum)
{
v[i]=v[j]=1;
printf("%d %d\n",i,j);
break;
}
} }
}
return 0;
}
2 seconds
256 megabytes
standard input
standard output
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 are not 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
On the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack.
这场比赛主要是想讲这题,大部分时间都在这题上了,其实思路很正确就是一些小细节导致时间白白浪费了;
题意:看样例就可以明白了,有点类似八皇后问题,n*n的象棋盘,每个象棋可以攻击与其同行或同列的旗子,问当放置第i个旗子后棋盘中不受攻击的象棋格子有多少;
思路:我们分几种情况就可以发现规律了,设有k1行已经被用过,k2列已经被用过,如果每次放置的旗子都与前面已经放好的旗子不同行且不同列,则剩下的不受攻击的格子有sum=sum-n-n+lie+hang+1;如果当前放置的旗子与前面某一放置好的旗子同行或同列,则sum=sum-n+(hang或lie);加上被用过的是因为前面已经减过了,再减就重复减了,所以要加回来;
#include<bits/stdc++.h>
using namespace std;
const int N=100000+10;
long long a[N],v1[N],v2[N];//注意数据范围;
int main()
{
long long n,m;
int i;
while(~scanf("%I64d%I64d",&n,&m))
{
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
long long x,y,hang=0,lie=0;
long long sum=n*n;
for(i=1; i<=m; i++)
{
scanf("%I64d%I64d",&x,&y);
if(v1[x]&&!v2[y])//这个判断条件一定要写全,下面同;不然跪;
{
sum=sum-n+hang;
v2[y]=1;
lie++;
}
else if(v2[y]&&!v1[x])//当前列被用过,也就是在前面的旗子的攻击范围内;
{
sum=sum-n+lie;
v1[x]=1;
hang++;
}
else if(!v1[x]&&!x2[y])
{
sum=sum-n-n+hang+lie+1;
v1[x]=v2[y]=1;
hang++;
lie++;
}
if(sum<=0) sum=0;
a[i]=sum;
}
for( i=1; i<=m; i++)
{
printf("%I64d",a[i]);
if(i!=m)
printf(" ");
else
printf("\n");
}
}
return 0;
}