Codeforces Round #316 (Div. 2)

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 nm (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 ≤ n1 ≤ i ≤ m0 ≤ 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.

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 3
1 2 3
2 3 1
1 2 1
output
2
input
3 4
10 10 3
5 1 6
2 2 2
1 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.


有hack点。。。

#include <bits/stdc++.h>
using namespace std;
int n,m;
long long mp[2333][2333];
long long re[2333];
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            cin>>mp[i][j];
    }
    int p;
    for(int i=0;i<m;i++)
    {
        long long max=0;p=0;
        for(int j=1;j<=n;j++)
        {
            if(mp[i][j]>max)
            {
                max=mp[i][j];
                p=j;
            }
        }
        re[p]++;
    }
    long long  max=0;
    for(int i=0;i<m;i++)
    {
        if(re[i]>max)
        {
            max=re[i];
            p=i;
        }
    }
    cout<<p+1;
    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 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.

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


注意n=1 时

#include <bits/stdc++.h>
using namespace std;
long long m,n;
int main()
{
    cin>>n>>m;
    if(n==1)
    {
         cout<<"1";
         return 0;
    }
    if(m>n/2)cout<<m-1;
    else cout<<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 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.

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 xi and ci (1 ≤ xi ≤ nci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.

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

每加入一个新的,统计两侧后再更改


#include <bits/stdc++.h>
using namespace std;
#define Max 300010
int n,m;
char t[Max];
int main()
{
    cin>>n>>m;
    scanf("%s",t+1);
    int res=0;
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        if(t[i]=='.') flag++;
        else flag=0;
        if(flag>=2) res++;
    }
    char tt;int b;
    for(int i=1;i<=m;i++)
    {
        cin>>b;cin>>tt;

        if(tt=='.')
        {
            if(t[b]!='.')
            {
                if(t[b-1]=='.') res++;
                if(t[b+1]=='.') res++;
            }
            t[b]='.';
        }
        else
        {
            if(t[b]=='.')
            {
                if(t[b-1]=='.') res--;
                if(t[b+1]=='.') res--;
            }
            t[b]='a';
        }
        printf("%d\n",res);
    }
    return 0;
}

D. Tree Requests
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

The first line contains two integers nm (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 vihi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in thei-th query.

Output

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

Sample test(s)
input
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2
output
Yes
No
Yes
Yes
Yes
Note

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


统计每个节点插入树中的序号和最后一个subtree节点的序号,每个深度插入树的序号,一定是增序列,对于新加入的节点的对上一个点位运算可以得到一个当前子树的信息,对于每次查询可以用lower_bound找到子树的起始和终点,再^一次可以得到最后结果


#include <bits/stdc++.h>
using namespace std;
#define Max 500010
vector <int >p[Max],dep[Max],v[Max];
int cnt,n,m,in[Max],out[Max];
char s[Max];
void dfs(int u,int step)
{
    in[u]=cnt++;
    dep[step].push_back(in[u]);
    int k=v[step][v[step].size()-1];
    v[step].push_back(k^(1<<(s[u]-'a')));
    for(int i=0;i<p[u].size();i++)
    {
        dfs(p[u][i],step+1);
    }
    out[u]=cnt;
}
int cal(int n)
{
    int res=0;
    while(n>0)
    {
        res+=n%2;
        n/=2;
    }
    return res;
}
int main()
{
    cin>>n>>m;int t;
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&t);
        p[t].push_back(i);
    }
    scanf("%s",s+1);cnt=1;
    for(int i=1;i<=n;i++)
    {
        dep[i].push_back(0);
        v[i].push_back(0);
    }
    dfs(1,1);
    int x,d;
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&x,&d);
        int a=lower_bound(dep[d].begin(),dep[d].end(),in[x])-dep[d].begin()-1;
        int b=lower_bound(dep[d].begin(),dep[d].end(),out[x])-dep[d].begin()-1;
        if(cal(v[d][a]^v[d][b])<=1) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

E. Pig and Palindromes
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting ofn rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to n, and the columns — from left to right with numbers from 1 to m. Let's denote the cell at the intersection of the r-th row and the c-th column as (r, c).

Initially the pig stands in cell (1, 1), and in the end she wants to be in cell (n, m). Since the pig is in a hurry to get home, she can go from cell (r, c), only to either cell (r + 1, c) or (r, c + 1). She cannot leave the forest.

The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to bebeautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem).

Count the number of beautiful paths from cell (1, 1) to cell (n, m). Since this number can be very large, determine the remainder after dividing it by 109 + 7.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 500) — the height and width of the field.

Each of the following n lines contains m lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.

Output

Print a single integer — the number of beautiful paths modulo 109 + 7.

Sample test(s)
input
3 4
aaab
baaa
abba
output
3
Note

Picture illustrating possibilities for the sample test.


两端扫


#include <bits/stdc++.h>

using namespace std;
const int MAX=505;
const int MOD=1e9+7;
char s[MAX][MAX];
int f[MAX][MAX]={0};
int f_[MAX][MAX];
int i,j,m,n,k,dis;
struct point{
    int x,y;
};
point next_1(point a)
{
    if (a.y==1&&a.x<n)
        a.x++;
    else
        a.y++;
    return a;
}
point next_2(point a)
{
    if (a.x==n&&a.y>1)
        a.y--;
    else
        a.x--;
    return a;
}
point nex(point a)
{
    a.x--;
    a.y++;
    return a;
}
int main()
{
    cin>>n>>m;
    int ans=0;
    getchar();
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
            scanf("%c",&s[i][j]);
        getchar();
    }
    point a,b,p1,p2;
    a.x=a.y=1;
    b.x=n;b.y=m;
    int max_=(m+n)/2;
    if (s[1][1]==s[n][m])
        f[1][n]=1;
    else
        f[1][n]=0;
    if (m+n<=3)
    {
        cout<<f[1][n]<<endl;
        return 0;
    }
    for (dis=2;dis<=max_;dis++)
    {
        a=next_1(a);
        b=next_2(b);
        for (i=1;i<=500;i++)
            for (j=1;j<=500;j++)
            {
                f_[i][j]=f[i][j];
                f[i][j]=0;
            }
        for (p1=a;p1.y<=m&&p1.x>=1;p1=nex(p1))
        {
            for (p2=b;p2.y<=m&&p2.x>=1;p2=nex(p2))
                if (s[p1.x][p1.y]==s[p2.x][p2.y])
                {
                    f[p1.x][p2.x]=((f_[p1.x-1][p2.x]+f_[p1.x-1][p2.x+1])%MOD+(f_[p1.x][p2.x]+f_[p1.x][p2.x+1])%MOD)%MOD;
                    if (((p1.x==p2.x)&&(abs(p1.y-p2.y)<=1))||((p1.y==p2.y)&&(abs(p1.x-p2.x)<=1)))
                        ans=(ans+f[p1.x][p2.x])%MOD;
                }
        }
    }
    cout<<ans<<endl;
    return 0;
}





1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值