<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">A链接:http://codeforces.com/contest/519/problem/A</span>
A and B are preparing themselves for programming contests.
To train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger.
For each chess piece we know its weight:
- the queen's weight is 9,
- the rook's weight is 5,
- the bishop's weight is 3,
- the knight's weight is 3,
- the pawn's weight is 1,
- the king's weight isn't considered in evaluating position.
The player's weight equals to the sum of weights of all his pieces on the board.
As A doesn't like counting, he asked you to help him determine which player has the larger position weight.
The input contains eight lines, eight characters each — the board's description.
The white pieces on the board are marked with uppercase letters, the black pieces are marked with lowercase letters.
The white pieces are denoted as follows: the queen is represented is 'Q', the rook — as 'R', the bishop — as'B', the knight — as 'N', the pawn — as 'P', the king — as 'K'.
The black pieces are denoted as 'q', 'r', 'b', 'n', 'p', 'k', respectively.
An empty square of the board is marked as '.' (a dot).
It is not guaranteed that the given chess position can be achieved in a real game. Specifically, there can be an arbitrary (possibly zero) number pieces of each type, the king may be under attack and so on.
Print "White" (without quotes) if the weight of the position of the white pieces is more than the weight of the position of the black pieces, print "Black" if the weight of the black pieces is more than the weight of the white pieces and print "Draw" if the weights of the white and black pieces are equal.
...QK... ........ ........ ........ ........ ........ ........ ...rk...
White
rnbqkbnr pppppppp ........ ........ ........ ........ PPPPPPPP RNBQKBNR
Draw
rppppppr ...k.... ........ ........ ........ ........ K...Q... ........
Black
In the first test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals 5.
In the second test sample the weights of the positions of the black and the white pieces are equal to 39.
In the third test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals to 16.
分别记录一下吧。。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 0x3fffff
int w=0,b=0;
void judge(char t)
{
if(t=='q') b+=9;
else if(t=='r') b+=5;
else if(t=='b'||t=='n') b+=3;
else if(t=='p') b+=1;
else if(t=='Q') w+=9;
else if(t=='R') w+=5;
else if(t=='B'||t=='N') w+=3;
else if(t=='P') w+=1;
}
int main()
{
w=0;
b=0;
char tt;
for(int i=1;i<=64;i++)
{
cin>>tt;
judge(tt);
}
if(b>w) cout<<"Black";
else if(w>b) cout<<"White";
else cout<<"Draw";
return 0;
}
A and B are preparing themselves for programming contests.
B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.
Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.
However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.
Can you help B find out exactly what two errors he corrected?
The first line of the input contains integer n (3 ≤ n ≤ 105) — the initial number of compilation errors.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the errors the compiler displayed for the first time.
The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one.
The fourth line contains n - 2 space-separated integers с1, с2, ..., сn - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.
Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.
5 1 5 8 123 7 123 7 5 1 5 1 7
8 123
6 1 4 3 3 5 7 3 7 5 4 3 4 3 7 5
1 3
In the first test sample B first corrects the error number 8, then the error number 123.
In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.
B:链接 点击打开链接
三次排序筛选出这两个数
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 0x3ffffffff
int n;
long a[233333],b[233333],c[233333];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++) cin>>b[i];
sort(b,b+n,cmp);
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
{
cout<<a[i]<<endl;
break;
}
}
for(int i=0;i<n-2;i++)
{
cin>>c[i];
}
sort(c,c+n,cmp);
for(int i=0;i<n-1;i++)
{
if(b[i]!=c[i])
{
cout<<b[i];
break;
}
}
return 0;
}
A and B are preparing themselves for programming contests.
An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.
A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.
However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.
As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.
There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?
The first line contains two integers n and m (0 ≤ n, m ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.
Print the maximum number of teams that can be formed.
2 6
2
4 5
3
Let's represent the experienced players as XP and newbies as NB.
In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).
In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).
只有1:2 和2:1 找差值大于最小数则为最小数,小于最小数则差值+最小值-差值/3*2如果模3==2再加一,应该算贪心?
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
int minn=min(a,b);
int maxx=max(a,b);
if(maxx>=2*minn) cout<<minn;
else
{
int sum=maxx-minn;
minn-=sum;
sum+=2*(minn/3);
if(minn%3==2) sum+=1;
cout<<sum;
}
return 0;
}
A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).
B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).
Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.
Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?
The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively.
The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.
Print the answer to the problem.
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 xabcab
2
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 aaa
2
In the first sample test strings satisfying the condition above are abca and bcab.
In the second sample test strings satisfying the condition above are two occurences of aa.
题意:给出26个字母分别代表的数值,和一个字符串。
求一个字符串首尾相同字母的并且除去这两个字母和为0的子串个数。
解法:建立一个map【i】【v】表示字母i当前的前缀和如果之前出现过则sum+=map。。map++
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
const int L=100100,M=30;
long long a[M]={};
char ch[L]={};
map<long long,int> m[M];
int main()
{
for(int i=0;i<26;++i)
cin>>a[i];
cin>>(ch+1);
int l=strlen(ch+1);
long long v=0,ans=0;
for(int i=1;i<=l;++i)
{
ans+=m[ch[i]-'a'][v];
v+=a[ch[i]-'a'];
++m[ch[i]-'a'][v];
}
cout<<ans<<endl;
return 0;
}
A and B are preparing themselves for programming contests.
The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.
Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.
As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following m days.
The first line contains integer n (1 ≤ n ≤ 105) — the number of rooms in the University.
The next n - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.
The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.
Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.
In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.
4 1 2 1 3 2 4 1 2 3
1
4 1 2 2 3 2 4 2 1 2 1 3
0 2
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
给定一棵树,给定m次询问,每次给2个点,问有多少个点到这两个点的距离是一样的
dfs先预处理出以某一个节点为根的子树里节点的数目,num[u]
然后在线lca
分几种情况就行了
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, n) FOR(i, 0, n)
#define sz(v) int((v).size())
#define pb push_back
#define all(e) (e).begin(), (e).end()
#define LSOne(S) (S & (-S)) // bit menos significativo
#define ii pair<int,int>
#define ll long long
const int MAXN = 100005;
vector<int> adj[MAXN];
int dis[MAXN];
int pre[MAXN], pos[MAXN];
int anc[MAXN];
int sum[MAXN];
bool pai (int u, int v) {
return pre[u]<=pre[v] && pos[v]<=pos[u];
}
int dfs (int v, int d, int &t) {
pre[v] = t++;
dis[v] = d;
sum[v] = 1;
FORR (i, sz(adj[v])) {
int k = adj[v][i];
if (pre[k]) continue;
anc[k] = v;
sum[v] += dfs (k, d+1, t);
}
pos[v] = t++;
return sum[v];
}
int mem[MAXN][20];
void calc (int n) {
memset (mem, -1, sizeof mem);
FORR (i, n) mem[i][0] = anc[i];
for (int j = 1; (1<<j) < n; j++) FORR (i, n) mem[i][j] = mem[mem[i][j-1]][j-1];
}
int find (int u, int k) {
int cnt = 0;
while (k) {
if (k&1) u = mem[u][cnt];
k >>= 1;
cnt++;
}
return u;
}
int lca (int u, int v) {
if (dis[u]>dis[v]) swap (u, v);
v = find (v, dis[v]-dis[u]);
if (u==v) return u;
int pvt = 0;
while (mem[u][pvt]!=mem[v][pvt]) pvt++;
while (pvt>=0) {
int x = mem[u][pvt], y = mem[v][pvt];
if (x!=y) u = x, v = y;
pvt--;
}
return mem[u][0];
}
int solve (int x, int y, int n) {
if (dis[x]>dis[y]) swap (x, y);
int l = lca (x, y);
int s = (dis[x]+dis[y]-2*dis[l]);
if (s&1) return 0;
s>>=1;
int k = find (y, s);
int c = 0;
int res = n;
if (s==0) return res;
if (dis[x]!=dis[y]) {
res -= sum[find(y,s-1)] + (n-sum[find(y,s)]);
} else {
res -= sum[find(y,s-1)] + sum[find(x,s-1)];
}
return res;
}
int main () {
int n;
while (scanf (" %d", &n) != EOF) {
FORR (i, n+1) adj[i].clear();
FORR (i, n-1) {
int x, y;
scanf (" %d %d", &x, &y);
x--, y--;
adj[x].pb (y);
adj[y].pb (x);
}
int t = 1;
memset (pre, 0, sizeof pre);
dfs (0, 0, t);
calc (n);
int m;
scanf (" %d", &m);
FORR (i, m) {
int x, y;
scanf (" %d %d", &x, &y);
x--, y--;
cout << solve (x, y, n) << endl;
}
}
}