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个数从中选取N/2对数使得每对数的和相等
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 110;
int num[maxn];
stack<int>S[maxn];
bool vis[maxn];
int main()
{
int n,sum=0;cin>>n;
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
sum+=num[i];
}
int avg=sum/(n/2);
for(int i=1;i<=n;++i){
if(!vis[i]){
vis[i]=true;
for(int j=i+1;j<=n;++j){
if(num[j]+num[i]==avg&&!vis[j]){
printf("%d %d\n",i,j);
vis[j]=true;break;
}
}
}
}
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 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.
记录收到攻击的所在行所在列即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<stack>
using namespace std;
const int maxn=100010;
bool visx[maxn];
bool visy[maxn];
int main()
{
long long n,m,x,y,ans,cntx=0,cnty=0;
cin>>n>>m;ans=n*n;
for(long long i=1;i<=m;++i){
scanf("%lld%lld",&x,&y);
if(!visx[x]&&!visy[y]){
ans=ans-(n-cntx+n-cnty-1);
visx[x]=visy[y]=true;
cntx++;cnty++;
}
else if(!visx[x]){
ans=ans-(n-cnty);cntx++;visx[x]=true;
}
else if(!visy[y]){
ans=ans-(n-cntx);cnty++;visy[y]=true;
}
printf("%lld\n",ans);
}
return 0;
}
Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.
There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.
Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.
The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.
The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.
Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
3 AaA
2
7 bcAAcbc
3
6 aaBCCe
5
In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.
In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.
In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<map>
#include<vector>
using namespace std;
const int maxn=1000010;
char str[maxn];
int vis[55];
int Find(char x){
if(x>='A'&&x<='Z'){
return x-'A'+26;
}
else {
return x-'a';
}
}
int main()
{
int n,cnt=0;cin>>n;
scanf("%s",str+1);
for(int i=1;i<=n;++i){
if(!vis[Find(str[i])]){
cnt++;vis[Find(str[i])]=1;
}
}
int ans=0,num=0,l=1,r;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;++i){
if(vis[Find(str[i])]==0){num++;}
ans++;r=i;vis[Find(str[i])]++;
if(num==cnt)break;
}
while(r<=n){
while(vis[Find(str[l])]>1){
vis[Find(str[l])]--;l++;
}
ans=min(ans,r-l+1);
r++;if(r>n)break;
vis[Find(str[r])]++;
}
printf("%d\n",ans);
return 0;
}
On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats fork people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.
Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected.
The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109,1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus.
Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
5 10 1 2 5
5.0000000000
3 6 1 2 1
4.7142857143
In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
题意:有N个人想要通过长为l的道路,每个人步行的速度为v1有一辆汽车每次可以乘坐k个人速度为v2求最短多长时间能将所有人走完这段道路。
思路:要使时间最短即让每个人和车每时每刻都在移动着。即所有人和车同时到达终点。且每个人坐车有且仅有一次。设每个人坐车所走的距离为l1 ,则有步行的距离为l-l1
设则有第一组同学下车地点为l1 所用时间t1=l1/v2 此时剩下的同学前进了t1*v1 所以可以计算出第二组同学的上车地点为2*l1*v1/(v1+v2);如此重复可以得出最后一组同学的上车地点为2*(num-1)*v1*l1/(v1+v2);因为最后一组同学行走l1后汽车将不再使用即为最后一组同学的最后一段路程在汽车上度过由此的上车地点也为l-l1由此计算出l1即可算出总时间
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<vector>
using namespace std;
const int maxn=10010;
int main()
{
int n,k;
double l,v1,v2;
scanf("%d%lf%lf%lf%d",&n,&l,&v1,&v2,&k);
int num=n/k+(n%k?1:0);
double l1=((v1+v2)*l)/(2.0*v1*(num-1)+v1+v2);
double ans=(l-l1)/v1+l1/v2;
printf("%.7lf\n",ans);
return 0;
}