codeforces#316 div2

来源:互联网 发布:淘宝店铺怎么设置打折 编辑:程序博客网 时间:2024/05/22 04:49
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 lineaij (1 ≤ j ≤ n,1 ≤ i ≤ m,0 ≤ aij ≤ 109) denotes the number of votes for candidatej in cityi.

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.

1001 简单的模拟题,根据题意直接模拟就好(两轮选举);1、每行选最大的(有相同的按小标小的取~~)2、被选举最多的输出~同样有相同按小标小的输出:

代码:

#include<iostream>#include <stdio.h>#include<string.h>using namespace std;#define N 110int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        long long int a[N];        int b;        memset(a,0,sizeof(a));        for(int i=1;i<=m;i++)        {            long long max2=0;            int pos=1;            for(int j=1;j<=n;j++)            {                scanf("%d",&b);                if(max2<b)                {                    max2=b;                    pos=j;                }            }            a[pos]++;        }        long long int max1=0;        int pos=1;        for(int i=1;i<=n;i++)        {            if(max1<a[i])            {                max1=a[i];                pos=i;            }        }        printf("%d\n",pos);    }    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 ton. Let's assume that Misha chose numberm, and Andrew chose numbera.

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 ifm 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 ofa 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 from1 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 or3. The probability that Andrew wins is2 / 3. If Andrew choosesa = 3, the probability of winning will be1 / 3. Ifa = 1, the probability of winning is0.

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


数学题:直接判断 左右区间大小即可,注意特殊数据的判断

代码:

#include<iostream>#include <stdio.h>#include<string.h>using namespace std;#define N 110int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        if(n==m)        {            printf("%d\n",n==1?1:n-1);            continue;        }        int a=n-m;        printf("%d\n",a>m-1?m+1:m-1);    }    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 strings 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 valueci. 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 integerxi andci (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.")

简单题:注意在数据查询处理时只需要看改变位置的前后是否是'.'即可~不用再遍历一遍~~


代码:

#include<iostream>#include <stdio.h>#include<string.h>#include<string>using namespace std;#define N 100010char a[3*N];char b[3*N];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        scanf("%s",a);        int ans=0;        for(int i=0; i<n-1; i++)        {            if(a[i]=='.'&&a[i+1]=='.')ans++;        }        int pos;        char c;        for(int i=0; i<m; i++)        {            scanf("%d %c",&pos,&c);            if(n==1)            {                printf("0\n");            }            else if(a[pos-1]=='.'&&c!='.')            {                if(pos>=2&&pos<n)                    printf("%d\n",(ans-=((a[pos-2]=='.')+(a[pos]=='.'))));                else if(pos<n)                    printf("%d\n",(ans-=(a[pos]=='.')));                else if(pos>=2&&pos>=n)                    printf("%d\n",(ans-=(a[pos-2]=='.')));            }            else if(a[pos-1]!='.'&&c=='.')            {                if(pos>=2&&pos<n)                    printf("%d\n",(ans+=((a[pos-2]=='.')+(a[pos]=='.'))));                else if(pos<n)                    printf("%d\n",(ans+=(a[pos]=='.')));                else if(pos>=2&&pos>=n)                    printf("%d\n",(ans+=(a[pos-2]=='.')));            }            else                printf("%d\n",ans);            a[pos-1]=c;        }    }    return 0;}


0 0
原创粉丝点击