PS:水平有限(惭愧脸)比赛的时候只写了前四题。剩下的等会写了再更新= =
E貌似也不难,但还没调对。
There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2people 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.
题意:就是保证每个人手里的牌的和一样。而且保证有解。就排序组合一下咯。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#define MAXN 110
using namespace std;
struct c{
int ID;
int num;
};
c a[MAXN];
int cmp(c x,c y)
{
return x.num<y.num;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i].num);
a[i].ID=i+1;
}
sort(a,a+n,cmp);
for(int i=0;i<n/2;i++)
{
printf("%d %d\n",a[i].ID,a[n-i-1].ID);
}
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
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.
题意:大概就是会在棋盘上放一些rook,rook可能会攻击自己这一行和这一列的格子。问每放一个rook,还剩下不会被攻击的格子有几个。
思路:每次统计被占领的行和列,比如为s1和s2,那么总共被攻击的就是n*n-(s1+s2)*n+s1*s2,交叉位置的格子重复计算了。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#define MAXN 100010
using namespace std;
int x[MAXN];
int y[MAXN];
int d1[MAXN]={0};
int d2[MAXN]={0};
vector<int> r;
vector<int> c;
int main()
{
long long n;
long long m;
scanf("%I64d%I64d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",x+i,y+i);
if(!d1[x[i]]){
d1[x[i]]=1;
r.push_back(x[i]);
}
if(!d2[y[i]]){
d2[y[i]]=1;
c.push_back(y[i]);
}
long long s1=(long long)r.size();
long long s2=(long long)c.size();
long long res=n*(n-s1-s2)+s1*s2;
printf("%I64d\n",res);
}
}
思路:裸的尺取法。s和t分别记录首尾,然后往后推移。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#define MAXN 100010
using namespace std;
char a[MAXN];
int p[MAXN];
int n;
set <int> all;
map <int,int> cnt;
int main()
{
scanf("%d",&n);
getchar();
scanf("%s",a);
int len=(int)strlen(a);
for(int i=0;i<len;i++)
{
p[i]=a[i]-'A';
all.insert(p[i]);
}
int m=(int)all.size();
int s=0,t=0,num=0;
int res=n;
while(1)
{
while(t<n&&num<m){
if(cnt[p[t++]]++==0)
{
num++;
}
}
if(num<m)
break;
res=min(res,t-s);
if(--cnt[p[s++]]==0)
num--;
}
printf("%d\n",res);
}
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 for k 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.
题意:一群学生,要到离这里为l的地方去。有一辆车,车只有k个座位。人和车的速度分别v1,v2,问你所有人到达的最小时间。
思路:数学题。最小时间就是要求所有同学同时到达。每个同学最多上一次车。那么显然总路程就是车走一段,人走一段,且每个同学两个距离相等。
那么怎么求呢?设每个人坐车距离为l1,步行就是l-l1,算一下一共需要车来回接多少次学生,记为cnt。
第一组学生上车地点在0处。然后会坐车往前 走l1,车再回来,去接后面的同学。
设第二组同学上车地点在d处。首先在车走到l1处时,所用的时间为t1=l1/v2。此时第二组和车相差距离d1=l1*(1-v1/v2)。那么相遇的时间t2=d1/(v1+v2)
那么第二组同学上车地点d会等于d=(t1+t2)*v1=2*v1*l1/(v1+v2).然后发生什么呢?车子往前走l1,再回来借第三组学生。
等等?这个过程是不是很眼熟?没错,和第一次是一样的。
所以每次前进的距离也是一样的。那么最后一组同学上车的位置也就是2*(cnt-1)*v1*l1/(v1+v2),同时也等于l-l1。
联立一下求出l1.T=l1/v2+(l-l1)/v2。
一定要注意精度问题!!!! 不是很明白的建议画个图一步步推一推。
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#define MAXN 100010
using namespace std;
int main()
{
int n,l,v1,v2,k;
scanf("%d%d%d%d%d",&n,&l,&v1,&v2,&k);
double res=0;
int cnt;
if(n%k==0)
cnt=n/k;
else
cnt=n/k+1;
double up=(double)(v1+v2)*l;
double down=(double)((2*(cnt-1)+1)*v1+v2);
double l1=(double)up/down;
res=(l-l1)/(double)v1+l1/(double)v2;
printf("%.12f\n",res);
}
以上。学长说E题只需要考虑每条边两边的子树分别包含的标记点最大值就好了,然后就能知道边被连了几次。所以最大值呢?。。。