A. Elections
The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.
Determine who will win the elections.
The first line of the input contains two integers n, m (1 ≤ n, m ≤ 100) — the number of candidates and of cities, respectively.
Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≤ j ≤ n, 1 ≤ i ≤ m, 0 ≤ aij ≤ 109) denotes the number of votes for candidate j in city i.
It is guaranteed that the total number of people in all the cities does not exceed 109.
Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.
3 3
1 2 3
2 3 1
1 2 1
2
3 4
10 10 3
5 1 6
2 2 2
1 5 7
1
Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.
Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index.
题解:O(n+m)模拟
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<cstring> 8 #define PAU putchar(' ') 9 #define ENT putchar(' ') 10 using namespace std; 11 const int maxn=100+10; 12 int A[maxn][maxn],n,m,mx[maxn],cnt[maxn]; 13 inline int read(){ 14 int x=0;bool sig=1;char ch=getchar(); 15 for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0; 16 for(;isdigit(ch);ch=getchar())x=10*x+ch-'0'; 17 return sig?x:-x; 18 } 19 inline void write(int x){ 20 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 21 int len=0,buf[20];while(x)buf[len++]=x%10,x/=10; 22 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 23 } 24 int main(){ 25 n=read();m=read(); 26 for(int i=1;i<=m;i++){ 27 for(int j=1;j<=n;j++){ 28 A[i][j]=read(); 29 } 30 } 31 for(int i=1;i<=m;i++){ 32 mx[i]=1; 33 for(int j=2;j<=n;j++){ 34 if(A[i][j]>A[i][mx[i]])mx[i]=j; 35 } 36 cnt[mx[i]]++; 37 } 38 int winer=1; 39 for(int i=2;i<=n;i++)if(cnt[i]>cnt[winer])winer=i;write(winer); 40 return 0; 41 }
B. Simple Game
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.
Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).
The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.
Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
3 1
2
4 3
2
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.
In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.
题解:还是很简单的啊?窝萌的最优策略YY一下肯定是贴着那个人呗,那么分情况讨论一下就好喽。注意n==1的情况。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<cstring> 8 #define PAU putchar(' ') 9 #define ENT putchar(' ') 10 using namespace std; 11 inline int read(){ 12 int x=0;bool sig=1;char ch=getchar(); 13 for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0; 14 for(;isdigit(ch);ch=getchar())x=10*x+ch-'0'; 15 return sig?x:-x; 16 } 17 inline void write(int x){ 18 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 19 int len=0,buf[20];while(x)buf[len++]=x%10,x/=10; 20 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 21 } 22 double n,m; 23 int main(){ 24 cin>>n>>m;double mid=(n+1)*0.5; 25 if(n==1){ 26 write(1); 27 return 0; 28 } 29 if(n==2){ 30 write(3-m); 31 return 0; 32 } 33 if(m>=mid)write(m-1); 34 else write(m+1); 35 return 0; 36 }
C. Replacement
Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacementas the following sequence of steps: find a substring ".." (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.
Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.
You need to process m queries, the i-th results in that the character at position xi (1 ≤ xi ≤ n) of string s is assigned value ci. After each operation you have to calculate and output the value of f(s).
Help Daniel to process all queries.
The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries.
The second line contains string s, consisting of n lowercase English letters and period signs.
The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci (1 ≤ xi ≤ n, ci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.
Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.
10 3
.b..bz....
1 h
3 c
9 f
4
3
1
4 4
.cc.
2 .
3 .
2 a
1 a
1
3
1
1
Note to the first sample test (replaced periods are enclosed in square brackets).
The original string is ".b..bz....".
- after the first query f(hb..bz....) = 4 ("hb[..]bz...." → "hb.bz[..].." → "hb.bz[..]." → "hb.bz[..]" → "hb.bz.")
- after the second query f(hbс.bz....) = 3 ("hbс.bz[..].." → "hbс.bz[..]." → "hbс.bz[..]" → "hbс.bz.")
- after the third query f(hbс.bz..f.) = 1 ("hbс.bz[..]f." → "hbс.bz.f.")
Note to the second sample test.
The original string is ".cc.".
- after the first query: f(..c.) = 1 ("[..]c." → ".c.")
- after the second query: f(....) = 3 ("[..].." → "[..]." → "[..]" → ".")
- after the third query: f(.a..) = 1 (".a[..]" → ".a.")
- after the fourth query: f(aa..) = 1 ("aa[..]" → "aa.")
题解:好囧的题。。。比赛的时候就想起来那道序列的题了。。。于是开始敲分块啊。。。各种讨论啊。。。伤不起啊。。。
早晨起来这么一想,窝萌为啥要维护块啊哥哥。。。修改操作的影响是很小的啊。。。一开始统计完了不就行了嘛。。。呜呜呜~~~~(>_<)~~~~
于是来一发计算贡献就水过了。。。。。。。。。。。有个细节:窝萌不需要讨论完整,无意义的变换(比如点换点之类的。。。)可以让程序自己抵消哦。。。
比赛的时候代码能力喂狗了。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<cstring> 8 #define PAU putchar(' ') 9 #define ENT putchar(' ') 10 using namespace std; 11 const int maxn=300000+10; 12 char s[maxn]; 13 char str[11]; 14 inline int read(){ 15 int x=0;bool sig=1;char ch=getchar(); 16 for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0; 17 for(;isdigit(ch);ch=getchar())x=10*x+ch-'0'; 18 return sig?x:-x; 19 } 20 inline void write(int x){ 21 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 22 int len=0,buf[20];while(x)buf[len++]=x%10,x/=10; 23 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 24 } 25 int main(){ 26 int n,m; cin >> n >> m; 27 28 scanf("%s",s); 29 30 int res = 0; 31 32 for (int i = 0; i < n - 1; i++) if (s[i] == s[i + 1] && s[i] == '.') { 33 res++; 34 } 35 36 while(m--) { 37 int pos; scanf("%d%s",&pos,str); --pos; 38 if (pos > 0 && s[pos] == s[pos - 1] && s[pos] == '.') res--; 39 if (pos + 1 < n && s[pos] == s[pos + 1] && s[pos] == '.') res--; 40 s[pos] = str[0]; 41 if (pos > 0 && s[pos] == s[pos - 1] && s[pos] == '.') res++; 42 if (pos + 1 < n && s[pos] == s[pos + 1] && s[pos] == '.') res++; 43 printf("%d ",res); 44 } 45 return 0; 46 }
D. Tree Requests
Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).
The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.
We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.
Roma gives you m queries, the i-th of which consists of two numbers vi, hi. Let's consider the vertices in the subtree vi located at depthhi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.
The first line contains two integers n, m (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.
The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).
The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.
Next m lines describe the queries, the i-th line contains two numbers vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in thei-th query.
Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2
Yes
No
Yes
Yes
Yes
String s is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome.
Clarification for the sample test.
In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".
In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them.
In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.
In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.
In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".
题解:dfs一遍,把东西扔到vector里,维护一下括号序,询问时二分vector。
1 #include <bits/stdc++.h> 2 3 #define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0) 4 char _; 5 6 using namespace std; 7 8 int N, M; 9 char S[500002]; 10 vector<int> occ[500001][26]; 11 int in[500001], out[500001], now; 12 struct ted{int x,y;ted*nxt;}adj[500000+10],*fch[500000+10],*ms=adj; 13 14 void add(int x,int y){ 15 *ms=(ted){x,y,fch[x]};fch[x]=ms++; 16 } 17 18 void dfs(int u, int depth) 19 { 20 occ[depth][S[u]-'a'].push_back(++now); 21 in[u]=now; 22 for(ted*e=fch[u];e;e=e->nxt) 23 dfs(e->y, depth+1); 24 out[u]=now; 25 } 26 27 int main() 28 { 29 scan(N); 30 scan(M); 31 for(int i=2; i<=N; i++) 32 { 33 int a; 34 scan(a);add(a,i); 35 } 36 scanf("%s", S+1); 37 dfs(1, 1); 38 while(M--) 39 { 40 int v, h; 41 scan(v); 42 scan(h); 43 int odd=0; 44 for(int i=0; i<26; i++) 45 { 46 int cnt=upper_bound(occ[h][i].begin(), occ[h][i].end(), out[v])- 47 upper_bound(occ[h][i].begin(), occ[h][i].end(), in[v]); 48 if(cnt%2==1) 49 { 50 odd++; 51 if(odd>1) 52 break; 53 } 54 } 55 if(odd>1) 56 printf("No "); 57 else 58 printf("Yes "); 59 } 60 return 0; 61 }