~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.

题意:给出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 #180 div2 C Parity Game

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

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

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

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

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

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

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

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

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

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

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

  7. Codeforces #263 div2 解题报告

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

  8. codeforces #round363 div2.C-Vacations (DP)

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

  9. codeforces round367 div2.C (DP)

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

随机推荐

  1. Android ORM -- Litepal(1)

    ORM,即Object Relation Mapping,对象关系映射,实现了程序里面的类和数据库里面的数据之间的对应关系,对数据库的操作可以通过对类的操作去实现,不用再写SQL语句,从而提高了开发效 ...

  2. CentOS6.8 修改主机名(1)

    1.临时修改主机名   显示主机名:spark@master:~$ hostnamemaster修改主机名:spark@master:~$ sudo hostname hadoopspark@mast ...

  3. source /etc/profile报错-bash: id:command is not found

    由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...

  4. 兼容IE8 input的placeholder的文字显示

    if( !('placeholder' in document.createElement('input')) ){ $('input[placeholder],textarea[placeholde ...

  5. CSS选择器的权重与优先规则?

    我们做项目的时候,经常遇到样式层叠问题,被其他的样式覆盖,或者写的权重不高没效果,对权重没有具体的分析,做了一个总结. css继承是从一个元素向其后代元素传递属性值所采用的机制.确定应当向一个元素应用 ...

  6. FineReport实现EXCEL数据导入自由报表

    在制作填报报表的时候,对于空白填报表,常常导出为Excel,派发给各部门人员填写后上交.如何能避免手动输入,直接将Excel中的数据导入到填报表中提交入库呢? 这里以一个简单的员工信息填报示例进行介绍 ...

  7. Android 从零开始打造异步处理框架

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/5995752.html 本文出自[赵彦军的博客] 概述 在Android中会使用异步任务来处理耗时操作,避免出 ...

  8. favicon.ico 404

    favicon.ico是一个图标文件.就是浏览网站时显示在地址栏的那个图标. 类似是百度的 显示在网站地址最前面的一张图片 可以在网站根目录(TOMCAT_HOME/webapps/ROOT/favi ...

  9. 由用友NC刷新功能得到启示

    在做NC刷新界面数据的功能,就是 点刷新按钮然后刷新当前 list 中的数据,犯了一个错误. 我之前的做法是记录每次查询数据的sql条件语句,然后在点刷新的时候去调用这个条件语句,哈哈,错误就在这个逻 ...

  10. (转载) 浅谈python编码处理

    最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些,我还是决定用中文输出日志信息. 很快,我就遇到了异常: UnicodeEncodeError: ...