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 to 8.
In the second sample, all values ai are equal. Thus, any distribution is acceptable.
题意:n个卡牌,现在希望把这些卡牌配对,使每一对的和相同。
思路:求平均暴力。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN],n,ans[MAXN],vis[MAXN];
vector<int>s;
int main(){
while(~scanf("%d",&n)){
int sum = ;
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
sum += a[i];
}
s.clear();
sum /= (n / );
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++){
if(vis[i])continue;
vis[i] = ;
for(int j = ; j <= n; j++){
if(!vis[j] && a[i] + a[j] == sum){
ans[i] = j;
vis[j] = vis[i] = ;
s.push_back(i);
break;
}
}
}
for(int i = ; i < s.size(); i++){
printf("%d %d\n",s[i],ans[s[i]]);
}
}
return ;
}
~B题
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 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
题意:n*n的棋盘,放m个棋子,每个棋子的攻击范围是一行一列,问每次放一颗后还有多少个位置没有被攻击过。
思路:我们统计能够被攻击的有多少个点。对于放棋子,我们加的是它每一行每一列的贡献,肯定室友重复的添加的,现在要删去的就是这些重复的位置。重复的位置有之前放好的棋子对应的列数和行数。处理一下细节。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int vis[MAXN][],n,m;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(vis,,sizeof(vis));
ll ans = ;
int rownum = ,colnum = ;
for(int i = ; i <= m; i++){
int x,y,flag1 = ,flag2 = ;
scanf("%d%d",&x,&y);
if(!vis[y][]){
vis[y][] = ; ans += n; flag1 ++;
ans -= rownum;
if(vis[x][])ans += ;
}
if(!vis[x][]){
vis[x][] = ; ans += n; flag2 ++;
ans -= colnum;
if(!flag1)ans += ;
}
if(vis[x][] && vis[y][] && (flag1 || flag2)){ans -= ;}
if(flag1)colnum ++;
if(flag2)rownum ++;
//cout<<ans<<' '<<endl;
printf("%lld ",1LL*n*n - ans);
}
puts("");
}
return ;
}
~C题
C. They Are Everywhere
2 seconds
256 megabytes
standard input
standard output
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 numbern - 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
题意:求出最短的一段区间,区间内出现了所有字符串中的字符。
思路:尺取法,维护2个指针。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
char s[MAXN];
int n,vis[];
int main(){
while(~scanf("%d",&n)){
int all = ;
scanf("%s",s+);
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++){
if(!vis[s[i]]){all += , vis[s[i]] = ;}
}
memset(vis,,sizeof(vis));
int num = ;
int k,j;
int minlen = INF,flag = ;
k = ;
j = ;
for(; j <= n;){
if(!flag){
if(!vis[s[j]]){num ++;}
vis[s[j]] ++;
if(num == all){minlen = min(minlen,j - k + );flag = ;continue;}
j ++;
} else {
while(k <= j){
if(num == all)
minlen = min(minlen,j - k + );
if(vis[s[k]] - > ){
vis[s[k]] --;
k ++;
} else {
vis[s[k]] = ;
k ++;
num -= ;
flag = ; j += ; break;
}
}
}
}
if(j > n)j = n;
//cout<<j<<' '<<k<<' '<<num<<endl;
while(k <= j){
if(num == all)
minlen = min(minlen,j - k + );
if(vis[s[k]] - > ){
vis[s[k]] --;
k ++;
} else {
break;
}
}
printf("%d\n",minlen);
}
return ;
}
~D题
追逐问题,初中要么高中的题目。
~E题
3 seconds
256 megabytes
standard input
standard output
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, ..., u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Print the maximum possible sum of distances in the division of universities into k pairs.
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
6
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
9
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
题意:给出n个地方,有n-1条路连接着这些地方。有2*k个大学坐落在这些地方。现在要求让这些大学配对,让这些大学间通网络。问最大的网线距离是多少。
思路:让2*k个地点相连,距离是一定的,因为原图是一棵树。所以现在考虑每一条边,对于边u --> v,如果v集合中有x个学校,u中有y个学校,那么从这条边最多被访问min(x,y)次。
所以可以一遍dfs来求得答案。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int next;
}edge[MAXN*];
int ind,pre[MAXN],vis[MAXN];
ll ans,num[MAXN];
int n,k;
void add(int x,int y){
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
vis[rt] = ;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dfs(t);
num[rt] += num[t];
ans += min(num[t],*k-num[t]);
}
}
}
int main(){
while(~scanf("%d%d",&n,&k)){
memset(num,,sizeof(num));
for(int i = ; i <= * k; i++){
int x;
scanf("%d",&x);
num[x] = ;
}
ind = ;
memset(pre,-,sizeof(pre));
for(int i = ; i < n; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
ans = ;
memset(vis,,sizeof(vis));
dfs();
printf("%lld\n",ans);
}
return ;
}
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lsonl,m,rt<<1
#define key_valuech[ch[root][1]][0]
#define rsonm+1,r,rt<<1|1
#define piacos(-1.0)
using namespace std;
const int MAXN = 100010;
int vis[MAXN][2],n,m;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(vis,0,sizeof(vis));
llans = 0;
int rownum = 0,colnum = 0;
for(int i = 1; i <= m; i++){
int x,y,flag1 = 0,flag2 = 0;
scanf("%d%d",&x,&y);
if(!vis[y][1]){
vis[y][1] = 1; ans += n; flag1 ++;
ans -= rownum;
if(vis[x][0])ans += 1;
}
if(!vis[x][0]){
vis[x][0] = 1; ans += n; flag2 ++;
ans -= colnum;
if(!flag1)ans += 1;
}
if(vis[x][0] && vis[y][1] && (flag1 || flag2)){ans -= 1;}
if(flag1)colnum ++;
if(flag2)rownum ++;
//cout<<ans<<' '<<endl;
printf("%lld ",1LL*n*n - ans);
}
puts("");
}
return 0;
}