Codeforces Round #316 (Div. 2)

来源:互联网 发布:淘宝搜索引擎工作流程 编辑:程序博客网 时间:2024/05/21 15:03
A. Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 thei-th line aij (1 ≤ j ≤ n,1 ≤ i ≤ m, 0 ≤ aij ≤ 109) denotes the number of votes for candidatej in city i.

It is guaranteed that the total number of people in all the cities does not exceed109.

Output

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

Sample test(s)
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.


题意:有n个人在m个城市里进行选举,每个城市里获得票数最多的那个人就会增加一颗星,如果有多个人都是最多的,那么就给号码(下标)最小的那个人加一个星。求获得星标最多的那个人编号,同样的,如果多个人都是最多的,那么输出编号最小的那个。

题目链接:http://codeforces.com/contest/570/problem/A

代码清单:

#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;int n,m;int a[105][105];int vis[105];int main(){    cin>>n>>m;    for(int i=1;i<=m;i++)        for(int j=1;j<=n;j++)        cin>>a[i][j];    for(int i=1;i<=m;i++){        int idx=1;        for(int j=2;j<=n;j++){            if(a[i][j]>a[i][idx]){                idx=j;            }        }        vis[idx]++;    }    int ans=1;    for(int i=2;i<=n;i++){        if(vis[i]>vis[ans]) ans=i;    }    cout<<ans<<endl;    return 0;}

B. Simple Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from1 to n. Let's assume that Misha chose numberm, 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 toc. The boys agreed that if m and a are located on the same distance fromc, Misha wins.

Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and numbern. 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, wherec is the equiprobably chosen integer from 1 to n (inclusive).

Input

The first line contains two integers n andm (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.

Sample test(s)
Input
3 1
Output
2
Input
4 3
Output
2
Note

In the first sample test: Andrew wins if c is equal to2 or 3. The probability that Andrew wins is2 / 3. If Andrew chooses a = 3, the probability of winning will be1 / 3. If a = 1, the probability of winning is0.

In the second sample test: Andrew wins if c is equal to1 and 2. The probability that Andrew wins is1 / 2. For other choices of a the probability of winning is less.

题意:给你n、m,求使得出现的概率最大的a (1 <= c <= n,任何从1到n的整数选择相同的概率)。如果有多个答案,输出最小的那个。

分析:此题是本次比赛最欢乐的一题。各种hack各种wa。有两坑。

第一坑:输入n = 1,m = 1,则应输出答案 1;

第二坑:当n是奇数时,如果m是中位数即m = (n+1)/2,此时有两个答案满足,一个m-1,;另一个m+1,题目要求输出小的,所以输出m-1。

题目链接:http://codeforces.com/contest/570/problem/B

代码清单:

#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;int n,m;int main(){    cin>>n>>m;    int ans;    if(m==1&&n==1) ans=1;    else if(n%2){        if(m<(n+1)/2) ans=m+1;        else ans=m-1;    }    else{         if(m<=(n+1)/2) ans=m+1;        else ans=m-1;    }    cout<<ans<<endl;    return 0;}


C. Replacement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation ofreplacement as the following sequence of steps: find a substring ".." (two consecutive periods) in strings, 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 ofreplacement 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 strings is assigned value ci. After each operation you have to calculate and output the value off(s).

Help Daniel to process all queries.

Input

The first line contains two integers n andm (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries.

The second line contains string s, consisting ofn lowercase English letters and period signs.

The following m lines contain the descriptions of queries. Thei-th line contains integer xi and ci (1 ≤ xi ≤ n,ci — a lowercas English letter or a period sign), describing the query of assigning symbolci to positionxi.

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.

Sample test(s)
Input
10 3.b..bz....1 h3 c9 f
Output
431
Input
4 4.cc.2 .3 .2 a1 a
Output
1311
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.")

题意:给你一个字符串,由‘.’和小写字母组成。把两个相邻的‘.’替换成一个‘.’,算一次变换。现在给你一些个操作,操作内容是把某个位置的字符变成给定的字符,求出每次操作后,需要多少次变换才能把原串所有相邻的‘.’变成一个‘.’。注意,每次操作是累加的,即一次操作就会把原串替换一个字符。

分析:此题可以这样想,首先计算出把原串变换需要ans次(易得)。对于一次操作,变换次数的增减仅跟当前要替换的字符的左右两边相邻的两个字符有关:

1)若 当前字符 != ‘.’ and 新字符 == ‘.’,如果 左边字符是 ‘.’,那么ans++;如果右边字符是‘.’,ans++;

2)若 当前字符 == ‘.’ and 新字符 != ‘.’,如果 左边字符是 ‘.’,那么ans--;如果右边字符是’.‘,ans--;

attention:别忘了每次操作把当前字符替换成新字符!!!

题目链接:http://codeforces.com/contest/570/problem/C

代码清单:

#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 300000 + 5;int n,m,p;char str[maxn],s[3];int dp1[maxn],dp2[maxn];int head[maxn],tail[maxn];int main(){    scanf("%d%d",&n,&m);    scanf("%s",str);    int ans=0;    for(int i=0;i<n;i++){        if(str[i]=='.'){            int an=0;            while(str[i]=='.'&&i<n){                an++;                i++;            }            while(an>1){                ans+=an/2;                an=an/2+an%2;            }        }    }    for(int i=0;i<m;i++){        scanf("%d%s",&p,s);        p--;        if(s[0]=='.'&&str[p]!='.'){            if(p-1>=0&&str[p-1]=='.') ans++;            if(p+1<n&&str[p+1]=='.') ans++;        }        else if(s[0]!='.'&&str[p]=='.'){            if(p-1>=0&&str[p-1]=='.') ans--;            if(p+1<n&&str[p+1]=='.') ans--;        }        str[p]=s[0];        printf("%d\n",ans);    }    return 0;}




0 0
原创粉丝点击