Codeforces #364 DIV2

时间:2021-08-27 18:33:43
 
~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
6
1 5 7 4 4 3
output
1 3
6 2
4 5
input
4
10 10 10 10
output
1 2
3 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 = ;
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题

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 3
1 1
3 1
2 2
output
4 2 0 
input
5 2
1 5
5 1
output
16 9 
input
100000 1
300 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 = ;
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

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
3
AaA
output
2
input
7
bcAAcbc
output
3
input
6
aaBCCe
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 = ;
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题

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 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
output
6
input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 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.

Codeforces #364 DIV2

题意:给出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的更多相关文章

  1. Codeforces &num;180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces &num;541 &lpar;Div2&rpar; - E&period; String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces &num;541 &lpar;Div2&rpar; - F&period; Asya And Kittens(并查集&plus;链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces &num;541 &lpar;Div2&rpar; - D&period; Gourmet choice(拓扑排序&plus;并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces &num;548 &lpar;Div2&rpar; - D&period;Steps to One(概率dp&plus;数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces &num;312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. Codeforces &num;263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  8. codeforces &num;round363 div2&period;C-Vacations &lpar;DP&rpar;

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  9. codeforces round367 div2&period;C &lpar;DP&rpar;

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. 利用Scala语言开发Spark应用程序

    Spark内核是由Scala语言开发的,因此使用Scala语言开发Spark应用程序是自然而然的事情.如果你对Scala语言还不太熟悉,可 以阅读网络教程A Scala Tutorial for Ja ...

  2. MyBatis之CRUD

    1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...

  3. C&num; 文件和文件夹操作

    一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...

  4. Linux文件查找工具之find &OpenCurlyDoubleQuote;大宝剑”--转载

    原文地址:http://xinzong.blog.51cto.com/10018904/1749465 一.文件查找工具常用软件 locate: locate命令其实是find -name的另一种写法 ...

  5. linux 访问windows共享

    1. windows端建立一个用户user用于共享访问 2. 共享一个目录,设置user可以访问,并在windows系统中确认可以访问 3. linux端创建一个用于挂载共享目录的目录    mkdi ...

  6. python&lowbar;爬百度百科词条

    如何爬取? 明确目标:爬取百度百科,定初始百度词条:python,初始URL:http://baike.baidu.com/item/Python,爬取数据量为1000条,值爬取简介,标题,和简介中u ...

  7. Web从入门到放弃&lt&semi;7&gt&semi;

    从这章开始读<javascript高级程序设计> <1>typeof 返回字符串 / 类型 未定义:undefined 布尔:boolean 字符串:string 数值:num ...

  8. Vue命令行工具vue-cli

    前面的话 Vue.js 提供一个官方命令行工具,可用于快速搭建大型单页应用.该工具提供开箱即用的构建工具配置,带来现代化的前端开发流程.只需几分钟即可创建并启动一个带热重载.保存时静态检查以及可用于生 ...

  9. 浅谈&period;net中事务

    .net中的事务 关键几点 概念:1:什么是事务 2:什么时候用事务 3:基本的语法 (1): 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常 ...

  10. toString方法的用法

    public class JLDtoS {   public static void main(String[]args)   {    long a=123;    Long aa=new Long ...