~A题
A. Cards
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
61 5 7 4 4 3

output
1 36 24 5

input
410 10 10 10

output
1 23 4

Note

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 = 1010;
int a[MAXN],n,ans[MAXN],vis[MAXN];
vector<int>s;
int main(){while(~scanf("%d",&n)){int sum = 0;for(int i = 1; i <= n; i++){scanf("%d",&a[i]);sum += a[i];}s.clear();sum /= (n / 2);memset(vis,0,sizeof(vis));for(int i = 1; i <= n; i++){if(vis[i])continue;vis[i] = 1;for(int j = 1; j <= n; j++){if(!vis[j] && a[i] + a[j] == sum){ans[i] = j;vis[j] = vis[i] = 1;s.push_back(i);break;}}}for(int i = 0; i < s.size(); i++){printf("%d %d\n",s[i],ans[s[i]]);}}return 0;
}

~B题

B. Cells Not Under Attack
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3 31 13 12 2

output
4 2 0 

input
5 21 55 1

output
16 9 

input
100000 1300 400

output
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 = 100010;
int vis[MAXN][2],n,m;
int main(){while(~scanf("%d%d",&n,&m)){memset(vis,0,sizeof(vis));ll ans = 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;
}

~C题

C. They Are Everywhere

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3AaA

output
2

input
7bcAAcbc

output
3

input
6aaBCCe

output
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 = 100010;
char s[MAXN];
int n,vis[200];
int main(){while(~scanf("%d",&n)){int all = 0;scanf("%s",s+1);memset(vis,0,sizeof(vis));for(int i = 1; i <= n; i++){if(!vis[s[i]]){all += 1, vis[s[i]] = 1;}}memset(vis,0,sizeof(vis));int num = 0;int k,j;int minlen = INF,flag = 0;k = 1;j = 1;for(; j <= n;){if(!flag){if(!vis[s[j]]){num ++;}vis[s[j]] ++;if(num == all){minlen = min(minlen,j - k + 1);flag = 1;continue;}j ++;} else {while(k <= j){if(num == all)minlen = min(minlen,j - k + 1);if(vis[s[k]] - 1 > 0){vis[s[k]] --;k ++;} else {vis[s[k]] = 0;k ++;num -= 1;flag = 0; j += 1; break;}}}}if(j > n)j = n;//cout<<j<<' '<<k<<' '<<num<<endl;while(k <= j){if(num == all)minlen = min(minlen,j - k + 1);if(vis[s[k]] - 1 > 0){vis[s[k]] --;k ++;} else {break;}}printf("%d\n",minlen);}return 0;
}

~D题

追逐问题,初中要么高中的题目。

~E题

E. Connecting Universities
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the maximum possible sum of distances in the division of universities into k pairs.

Examples
input
7 21 5 6 21 33 24 53 74 34 6

output
6

input
9 33 2 1 6 5 98 93 22 73 47 64 52 12 8

output
9

Note

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 = 200010;
struct node{int to;int next;
}edge[MAXN*2];
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] = 1;for(int i = pre[rt]; i != -1; i = edge[i].next){int t = edge[i].to;if(!vis[t]){dfs(t);num[rt] += num[t];ans += min(num[t],2*k-num[t]);}}
}
int main(){while(~scanf("%d%d",&n,&k)){memset(num,0,sizeof(num));for(int i = 1; i <= 2 * k; i++){int x;scanf("%d",&x);num[x] = 1;}ind = 0;memset(pre,-1,sizeof(pre));for(int i = 1; i < n; i++){int x,y;scanf("%d%d",&x,&y);add(x,y),add(y,x);}ans = 0;memset(vis,0,sizeof(vis));dfs(1);printf("%lld\n",ans);}return 0;
}

#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;
}

转载于:https://www.cnblogs.com/sweat123/p/5726213.html

Codeforces #364 DIV2相关推荐

  1. codeforces#320(div2) D Or Game 贪心

    codeforces#320(div2) D  "Or" Game  贪心 D. "Or" Game time limit per test 2 seconds ...

  2. codeforces 628.div2

    # Codeforces 628.div2 A. EhAb AnD gCd B. CopyCopyCopyCopyCopy C. Ehab and Path-etic MEXs D. Ehab the ...

  3. codeforces#324(div2) E. Anton and Ira 贪心

    codeforces#324(div2) E. Anton and Ira  贪心 E. Anton and Ira time limit per test 1 second memory limit ...

  4. codeforces 712 div2 ABC

    codeforces 712 div2 ABC A. Déjà Vu A palindrome is a string that reads the same backward as forward. ...

  5. codeforces round div2,3周赛补题计划(从开学到期末)

    1. 本学期场次 从2020.09.19-2021.01.18,一共18周. 题号 场次 日期 备注 1475 Codeforces Round #697 (Div. 3) 1.25 1474 Cod ...

  6. Codeforces#371 Div2

    这是一场非常需要总结的比赛,交了3题,最后终测的时候3题全部没过,一下掉到了绿名,2333 Problem A 题意:给定区间[l1,r1],[l2,r2],然后给定一个整数k,求区间当中相交的元素, ...

  7. 【Codeforces #130 Div2】Solutions

    [208A  Dubstep] http://codeforces.ru/problemset/problem/208/A 题目大意:一个句子被添加了若干"WUB",问原句. 将W ...

  8. 付忠庆的练习小笔记-Codeforces #277 Div2 C

    原题链接 http://codeforces.com/contest/486/problem/C 这个C题显然更水一些 步数可以分为两种 上下一种 左右一种 总步数最小 = 上下最小+左右最小 先讨论 ...

  9. 付忠庆的练习小笔记-Codeforces #276 Div2 C

    原题链接 http://codeforces.com/contest/485/problem/C 题意:给出一个区间 l~r 求这个区间内的数中转换成2进制含'1'最多的数,若有多组解,则输出最小的那 ...

最新文章

  1. 在Redhat 5.0 上安装Eclipse 3.6
  2. 什么是缓存一致性问题?如何解决呢?
  3. 交付效率提升40%,珍爱网基于微服务的DevOps落地指南
  4. sscanf的常见用法
  5. 雄安大学真的来了!占地总面积2550亩!
  6. C/C++基础语法复习(一):C++与C语言的区别,主要有这些!
  7. 016医疗项目 数据字典(概念和在本系统中的应用)
  8. 使用函数自调用实现将局部变量转换成全局变量
  9. 95-240-040-原理-State-简介
  10. css matrix矩阵,矩阵 | matrix() (Transforms) - CSS 中文开发手册 - Break易站
  11. 3803. 数组去重-AcWing题库
  12. 计算机网络ABCD,计算机网络理及应用试卷(ABCDE).doc
  13. dell进入u盘启动模式_戴尔主板bios设置u盘启动
  14. 上上下下左右左右BA
  15. 怎么让小白理解intel处理器(CPU)的分类
  16. 电脑重装系统后无法上网怎么办
  17. 枚举,如何定义一个枚举,枚举的本质
  18. 供应科研试剂乙缩醛-琥珀酰亚胺酯,Acetal-NHS (SDMB)
  19. php qq昵称,自写与收集的一些免费的API接口(获取QQ昵称、头像、QQ秀等等...)...
  20. 电磁场与仿真软件(18)

热门文章

  1. NAS 百科 —— http://baike.baidu.com/item/NAS%E7%BD%91%E7%BB%9C%E5%AD%98%E5%82%A8
  2. 使用ASP.Net WebAPI构建REST服务(四)——参数绑定
  3. Oracle数据库的创建、数据导入导出
  4. Fedora 8安装非官方compiz-fusion
  5. 利用dynamoRIO实现codeCoverage
  6. 计算机组成原理之磁盘
  7. MapReduce之join操作
  8. python set并集update_Python中集合set()的使用及处理
  9. 【Arduino】OTTO机器人(做二次开发的一点点总结)
  10. Linux内核分析 - 网络[七]:NetFilter