2017-4-30-Train:Codeforces Round #316 (Div. 2)

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.

Input

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 a**ij (1 ≤ j ≤ n, 1 ≤ i ≤ m, 0 ≤ a**ij ≤ 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.

Output

Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.

Examples

input

3 31 2 32 3 11 2 1

output

2

input

3 410 10 35 1 62 2 21 5 7

output

1

Note

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.

Means:

有n个候选人和m个城市,现在要从m个城市中的选票中选取得票最高的候选人,如果在一个城市中两个候选人得到的票数是相同的且是最高的,那么标号小的候选人获胜,如果m个城市中选出的候选人的中有票数最高且相同的,依旧选标号最小的。

Solve:

直接记录每次一哪个最高然后最后直接判断?个中哪个最高就好了

Code:

 1 #pragma comment(linker, "/STACK:36777216")
 2
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define LSON            id << 1 , l , mid
 6 #define RSON            id << 1 | 1 , mid + 1 , r
 7 #define ROOT            1 , 1 , n
 8 #define CLR(x , y)      memset(x , y , sizeof(x))
 9 #define LOWBIT(x)       x & (-x)
10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
12 #define CASE(x)        printf("Case %d: ", x)
13 #define SFD(x)      scanf("%lf" , &x)
14 #define SFC(x)      scanf(" %c" , &x)
15 #define SFS(x)      scanf(" %s" , x)
16 #define SFI(x)      scanf("%d" , &x)
17 #define SFI64(x)    scanf("%I64d" , &x)
18 #define PFF(x)         printf("%f" , x)
19 #define PFD(x)         printf("%lf" , x)
20 #define PFI(x)         printf("%d" , x)
21 #define PFC(x)         printf("%c" , x)
22 #define PFS(x)         printf("%s" , x)
23 #define PFI64(x)       printf("%I64d" , x)
24 #define SPACE          printf(" ")
25 #define PUT            puts("")
26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
28 #define PB(x)          push_back(x)
29 #define ALL(A)         A.begin(), A.end()
30 #define SZ(A)          int((A).size())
31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
33 #define LOCAL
34 static const double PI = acos(-1.0);
35 static const double EPS = 1e-8;
36 static const int INF = 0X3fffffff;
37 typedef __int64 LL;
38 typedef double DB;
39 template<class T> inline
40 T read(T &x)
41 {
42     x = 0;
43     int f = 1 ; char ch = getchar();
44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
46     x *= f;
47 }
48
49 /************************Little Pea****************************/
50
51 static const int MAXN = 110;
52 LL vis[MAXN];
53 int main()
54 {
55 #ifndef ONLINE_JUDGE
56     freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
57 #endif
58     int n , m;
59     LL x;
60     read(n);read(m);
61     LPUP(i , 1 , m)
62     {
63         LL mx = -1;
64         int pos = -1;
65         LPUP(j , 1 , n)
66         {
67             read(x);
68             if(x > mx)
69             {
70                 mx = x;
71                 pos = j;
72             }
73         }
74         ++vis[pos];
75     }
76     LL mx = -1;
77     int pos = -1;
78     LPUP(i , 1 , n)
79     {
80         if(vis[i] > mx)
81         {
82             pos = i;
83             mx = vis[i];
84         }
85     }
86     PFI(pos);
87
88 #ifndef ONLINE_JUDGE
89     fclose(stdin), fclose(stdout);
90 #endif
91 }

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

Input

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.

Output

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.

Examples

input

3 1

output

2

input

4 3

output

2

Note

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.

Code:

 1 #pragma comment(linker, "/STACK:36777216")
 2
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define LSON            id << 1 , l , mid
 6 #define RSON            id << 1 | 1 , mid + 1 , r
 7 #define ROOT            1 , 1 , n
 8 #define CLR(x , y)      memset(x , y , sizeof(x))
 9 #define LOWBIT(x)       x & (-x)
10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
12 #define CASE(x)        printf("Case %d: ", x)
13 #define SFD(x)      scanf("%lf" , &x)
14 #define SFC(x)      scanf(" %c" , &x)
15 #define SFS(x)      scanf(" %s" , x)
16 #define SFI(x)      scanf("%d" , &x)
17 #define SFI64(x)    scanf("%I64d" , &x)
18 #define PFF(x)         printf("%f" , x)
19 #define PFD(x)         printf("%lf" , x)
20 #define PFI(x)         printf("%d" , x)
21 #define PFC(x)         printf("%c" , x)
22 #define PFS(x)         printf("%s" , x)
23 #define PFI64(x)       printf("%I64d" , x)
24 #define SPACE          printf(" ")
25 #define PUT            puts("")
26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
28 #define PB(x)          push_back(x)
29 #define ALL(A)         A.begin(), A.end()
30 #define SZ(A)          int((A).size())
31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
33 #define LOCAL
34 static const double PI = acos(-1.0);
35 static const double EPS = 1e-8;
36 static const int INF = 0X3fffffff;
37 typedef __int64 LL;
38 typedef double DB;
39 template<class T> inline
40 T read(T &x)
41 {
42     x = 0;
43     int f = 1 ; char ch = getchar();
44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
46     x *= f;
47 }
48
49 /************************Little Pea****************************/
50
51 int n , m;
52 int main()
53 {
54 #ifndef ONLINE_JUDGE
55     freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
56 #endif
57     read(n);read(m);
58     if(n == 1 && m == 1)
59     {
60         puts("1");
61         return 0;
62     }
63     int mid = n >> 1;
64     if(m > mid)
65     {
66         PFI(m - 1);
67     }
68     else
69     {
70         PFI(m + 1);
71     }
72
73 #ifndef ONLINE_JUDGE
74     fclose(stdin), fclose(stdout);
75 #endif
76 }

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 x**i (1 ≤ x**i ≤ n) of string s is assigned value c**i. After each operation you have to calculate and output the value of f(s).

Help Daniel to process all queries.

Input

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 x**i and c**i (1 ≤ x**i ≤ n, c**i — a lowercas English letter or a period sign), describing the query of assigning symbol c**i to position x**i.

Output

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.

Examples

input

10 3
.b..bz....
1 h
3 c
9 f

output

4
3
1

input

4 4
.cc.
2 .
3 .
2 a
1 a

output

1
3
1
1

Note

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.")

Code:

  1 #pragma comment(linker, "/STACK:36777216")
  2
  3 #include <bits/stdc++.h>
  4 using namespace std;
  5 #define LSON            id << 1 , l , mid
  6 #define RSON            id << 1 | 1 , mid + 1 , r
  7 #define ROOT            1 , 1 , n
  8 #define CLR(x , y)      memset(x , y , sizeof(x))
  9 #define LOWBIT(x)       x & (-x)
 10 #define FORN(i , a , n)  for(int i = (a) ; i <= (n) ; ++i)
 11 #define FORP(i , n , a)  for(int i = (n) ; i >= (a) ; --i)
 12 #define CASE(x)        printf("Case %d: ", x)
 13 #define SFD(x)      scanf("%lf" , &x)
 14 #define SFC(x)      scanf(" %c" , &x)
 15 #define SFS(x)      scanf(" %s" , x)
 16 #define SFI(x)      scanf("%d" , &x)
 17 #define SFI64(x)    scanf("%I64d" , &x)
 18 #define PFF(x)         printf("%f" , x)
 19 #define PFD(x)         printf("%lf" , x)
 20 #define PFI(x)         printf("%d" , x)
 21 #define PFC(x)         printf("%c" , x)
 22 #define PFS(x)         printf("%s" , x)
 23 #define PFI64(x)       printf("%I64d" , x)
 24 #define SPACE          printf(" ")
 25 #define PUT            puts("")
 26 #define LPUP(i , j , k) for(int i = j ; i <= k ; ++i)
 27 #define LPDW(i , j , k) for(int i = j ; i >= k ; --i)
 28 #define PB(x)          push_back(x)
 29 #define ALL(A)         A.begin(), A.end()
 30 #define SZ(A)          int((A).size())
 31 #define LBD(A, x)      (lower_bound(ALL(A), x) - A.begin())
 32 #define UBD(A, x)      (upper_bound(ALL(A), x) - A.begin())
 33 #define LOCAL
 34 static const double PI = acos(-1.0);
 35 static const double EPS = 1e-8;
 36 static const int INF = 0X3fffffff;
 37 typedef __int64 LL;
 38 typedef double DB;
 39 template<class T> inline
 40 T read(T &x)
 41 {
 42     x = 0;
 43     int f = 1 ; char ch = getchar();
 44     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
 45     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
 46     x *= f;
 47 }
 48
 49 /************************Little Pea****************************/
 50
 51 static const int MAXN = 3e5 + 10;
 52 char a[MAXN];
 53 int n , m;
 54 int ans;
 55 int main()
 56 {
 57 #ifndef ONLINE_JUDGE
 58     freopen("D:\\系统优化\\Desktop\\littlepea\\in.data" , "r" , stdin);
 59 #endif
 60     read(n);read(m);
 61     SFS(a + 1);
 62     LPUP(i , 2 , n)
 63     {
 64         if(a[i] == ‘.‘ && a[i - 1] == ‘.‘)
 65         {
 66             ++ans;
 67         }
 68     }
 69     int p;
 70     while(m--)
 71     {
 72         SFI(p);
 73         char ch[5] = {‘\0‘};
 74         scanf("%s" , ch + 1);
 75         if((a[p] == ‘.‘ && ch[1] == ‘.‘) || (a[p] != ‘.‘ && ch[1] != ‘.‘));
 76         else
 77         {
 78             if(ch[1] == ‘.‘)
 79             {
 80                 if(p != 1 && a[p-1] == ‘.‘)
 81                     ans++;
 82                 if(p != n && a[p+1] == ‘.‘)
 83                     ans++;
 84             }
 85             else
 86             {
 87                 if(p != 1 && a[p-1] == ‘.‘)
 88                     ans--;
 89                 if(p != n && a[p+1] == ‘.‘)
 90                     ans--;
 91             }
 92         }
 93         a[p] = ch[1];
 94         PFI(ans);PUT;
 95     }
 96
 97 #ifndef ONLINE_JUDGE
 98     fclose(stdin), fclose(stdout);
 99 #endif
100 }

时间: 2024-08-04 12:11:25

2017-4-30-Train:Codeforces Round #316 (Div. 2)的相关文章

Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点

题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点路径上的点数.每次询问a,ba,b查询以aa为根的子树内深度为bb的节点上的字母重新排列之后是否能构成回文串.分析:很明显是个树上启发式合并.显然,只要深度为bb结点的所有颜色中,至多有一种的数量为奇数就可以构成回文串了. #include<bits/stdc++.h> using namespa

Codeforces Round #316 (Div. 2) B. Simple Game

思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看下边界数据,不要急着交. 题目链接:http://codeforces.com/problemset/problem/570/B <pre name="code" class="cpp">#include<bits/stdc++.h> using

Codeforces Round #316 (Div. 2) C. Replacement

题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符,问每次须要多少次合并次数.才使字符串没有' .. ' 思路:最原始的想法,就是对于每一次询问,都遍历整个字符串.这样时间复杂度o(n*m),就高达10^10方,非常明显会tle. 换下思路,事实上每次询问所改变的字符都会保留到下一次.也就是下一次的次数就会受到上一次的影响,那么我仅仅要就算出第一次的

Codeforces Round #316 (Div. 2) 简单题解

简单小结:A题先找出每个城市中得票最高的然后再在以上求出的那帮人中出一个得票(一个城市只算一票)中最高的 1 /*********************************** 2 * * 3 * Auther Rhapsody * 4 * E-mail [email protected] * 5 * * 6 ***********************************/ 7 #include <set> 8 #include <map> 9 #include &l

Codeforces Round #316 (Div. 2) D、E

Problem D: 题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串 分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显然这个状态可以用二进制表示 那么对于单个询问如何快速找到所有符合要求的点呢? 这里可以考虑树的dfs序,我们把深度相同的点按照dfs序加入集合中,易知在同一颗子树中的点肯定形成了一个连续的区间. 因此每次可以通过二分子树根节点的进入dfs序的时间和出dfs序的时间来找到这个区间 找到区间后可以根据预

Codeforces Round #316 (Div. 2)

A - Elections 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,num[105],a[105][105]; 4 int main() 5 { 6 scanf("%d%d",&n,&m); 7 for(int i=1;i<=m;i++) 8 { 9 int item=1; 10 for(int j=1;j<=n;j++) 11 { 12 scanf("%d&

Codeforces Round #316 (Div. 2) A

Description 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 s

Codeforces Round #316 (Div. 2) D Tree Requests

官方题解是离线询问,dfs树形转线性,然后二分找区间. 还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案. 代码是学习别人的http://blog.csdn.net/squee_spoon/article/details/47666667 当时做的时候想得是树形转线性,觉得dfs会暴栈,想用bfs,之前又没写过,于是愣了一个钟头. #include<bits/stdc++.h> using namespace

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe