A:
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 to 8.
In the second sample, all values ai are equal. Thus, any distribution is acceptable.
题意:
n个数,任意两个数和相同,输出数的位置。
题解:
先求出这个”和“,遍历,找到后输出并标记。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define M 200005
#define inf 1000000007
using namespace std;
int a[102];
int b[102];
int main()
{
int t;
while(cin>>t)
{
mem(a);
mem(b);
int ans=0;
for(int i=1;i<=t;i++)
{
cin>>a[i];
ans+=a[i];
}
int sum=ans/(t/2);//和
for(int i=1;i<=t;i++)
{
for(int j=i+1;j<=t;j++)
{
if(a[i]+a[j]==sum&&b[i]==0&&b[j]==0)
{
cout<<i<<" "<<j<<endl;
b[i]=b[j]=1;
break;
}
}
}
}
}
B:
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的矩阵,m个车。每一个车,会把它所在行,所在列的棋子吃掉,问每放一个车,剩余的棋子数?
题解:
设矩阵(A,B)如果放的车的所在行所在列没有之前的车,则a--,b--,如果有,则不用减。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define M 100005
#define inf 1000000007
using namespace std;
int l[M];
int r[M];
long long sum[M];
int main()
{
int n,m,x,y;
long long ans;
while(cin>>n>>m)
{
mem(l);
mem(r);
mem(sum);
long long a,b;
a=b=n;
for(int i=1; i<=m; i++)
{
a--;
b--;
scanf("%d%d",&x,&y);
l[x]++;
r[y]++;
if(l[x]!=1)
a++;
if(r[y]!=1)
b++;
ans=a*b;
sum[i]=ans;
}
cout<<sum[1];
for(int i=2; i<=m; i++)
{
cout<<" "<<sum[i];
}
printf("\n");
}
}