Codeforces #364 DIV2
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;
}
Codeforces #364 DIV2的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- 学习之路~sqh
推荐博客 Edison Chou: Vamei: 算法∙面试专题 - 简书: 设计模式 极速理解设计模式系列[目录索引]- Caleung: Net设计模式 - 灵动生活: 宅男程序员给老婆的计算机课 ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 来玩Play框架03 模板
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在上一章节中,我把字符串通过ok()返回给客户.我可以把一个完整的html页面放入 ...
- 关于MySql的1045错误修正
很多情况数据库很久没有使用,偶尔打开会出现一系列错误,例如1045错误即是 mysql ERROR 1045 : Access denied for user‘root’@localhost(usin ...
- CString转换为LPSTR和LPSTR转化为CString
一.CString转换为LPSTR 方法一: CString strFileName LPSTR lpstr - strFileName.GetBuffer(); strFileName.Releas ...
- .Container与.container_fluid区别
.Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...
- tomcat项目中文乱码问题解决方法
在部署tomcat项目时经常会遇到中文乱码问题,解决的方法可参考以下步骤. 1.更改Tomcat安装目录下的conf\server.xml,指定浏览器的编码格式为"utf-8"格式 ...
- SharePoint 2013 设置网站集为”只读”
有时候当我们升级或者部署项目时,不希望用户在此期间操作SharePoint,比如上传文档. SharePoint提供了这样的功能:管理中心------应用程序管理------管理配额和锁定 完成后,再 ...
- android6.0的坑
虽然现在android已经出了7.0了.但是大部分人用的应该还是5.0和6.0的. 其中对于开发者来说,变化比较大的应该是6.0之前和6.0之后的版本. 因为以6.0为分界线多了一个比较坑的东西:权限 ...
- webform(八)——LinQ简单增、删、改、查
一.简单介绍 1.LinQ to Sql类(NET Language Integrated Query (LINQ) ) LINQ定义了大约40个查询操作符,如select.from.in.where ...