20200314VJ训练总结

1.Dima and a Bad XOR添加链接描述
Student Dima from Kremland has a matrix a of size n×m filled with non-negative integers.

He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!

Formally, he wants to choose an integers sequence c1,c2,…,cn (1≤cj≤m) so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds, where ai,j is the matrix element from the i-th row and the j-th column.

Here x⊕y denotes the bitwise XOR operation of integers x and y.
Input
The first line contains two integers n and m (1≤n,m≤500) — the number of rows and the number of columns in the matrix a.

Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. ai,j (0≤ai,j≤1023).
Output
If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print “NIE”.

Otherwise print “TAK” in the first line, in the next line print n integers c1,c2,…cn (1≤cj≤m), so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.

If there is more than one possible answer, you may output any.
Examples
Input
3 2
0 0
0 0
0 0
Output
NIE
Input
2 3
7 7 7
7 7 10
Output
TAK
1 3
Note
In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero.

In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7⊕10=13, 13 is more than 0, so the answer is found.
先找出第一列所有元素的异或,若不等于零,结束,否则,再任意一行中找出不与第一个元素相同的值即可。
0 ^ n = n
n ^ n => 0`

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#define N 1005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int i,j,k;
int flag1,flag2,step[N][N],n,m,t,Max=0,Min=INF;
ll cnt,len,sum,ans; 
bool cmp(int a,int b)
{   
    return a>b;     
} 
int main()
{    
    cin>>n>>m;    
    for(i=1;i<=n;i++)    
    {        
        for(j=1;j<=m;j++)        
        {            
            cin>>step[i][j];            
            if(j==1)                
                ans^=step[i][j];        
        }    
    }     
    if(ans)   
    {        
        cout<<"TAK"<<endl;        
        while(n--)        
        {            
            cout<<1<<" ";       
        }        
        cout<<endl;    
        }    
        else    
        {        
            for(i=1;i<=n;i++)        
            {            
                for(j=2;j<=m;j++)            
                {                
                    if(step[i][j]!=step[i][1])                
                    {                    
                        flag1=i;                    
                        flag2=j;                    
                        break;                
                    }            
                }            
                if(flag1)   break;        
            }     ~   
            if(flag1)        
            {            
                cout<<"TAK"<<endl;            
                for(i=1;i<=n;i++)            
                {                
                    if(i==flag1)                    
                    cout<<flag2<<" ";                
                    else  cout<<"1 ";            
                }            
                cout<<endl;        
            }        
        else       cout<<"NIE"<<endl;    
    }    
    return 0;
}

2.Flowers.
添加链接描述
We saw the little game Marmot made for Mole’s lunch. Now it’s Marmot’s dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.

But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.

Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

Input
Input contains several test cases.

The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases.

The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.

Output
Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).

Examples
Input
3 2
1 3
2 3
4 4
Output
6
5
5
Note
For K = 2 and length 1 Marmot can eat ®.
For K = 2 and length 2 Marmot can eat (RR) and (WW).
For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR).
For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can’t eat (WWWR).
典型DP思想,若吃掉i朵花,则可以在吃掉i-1朵花后再吃掉一朵红花,或吃掉i-k朵花后再吃k朵百花,即a[i]=a[i-1]+a[i-k]

#include<iostream>
#include<bits/stdc++.h>
#define mod 1000000007
#define N 100000+5
usi~ng~ namespace std;
typedef long long ll;
ll t,k,a[100001],p,b,i;
int main()
{
    cin>>t>>k;
    a[0]=0;
    for(i=1;i<=100000;i++)
    {
        if(i<k)
        {
            a[i]=1;
        }
        else if(i==k)
        {
            a[i]=2;
        }
        else
        {
            a[i]=(a[i-1]+a[i-k])%mod;
        }
    }
    for(i=2;i<=100000;i++)
    {
        a[i]=(a[i-1]+a[i]);
    }
    while(t--)
    {
        cin>>p>>b;
        cout<<(a[b]-a[p-1])%mod<<endl;
    }
    return 0;
}

3.Case of the Zeros and Ones
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.

Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.

Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.

Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.

The second line contains the string of length n consisting only from zeros and ones.

Output
Output the minimum length of the string that may remain after applying the described operations several times.

Examples
Input
4
1100
Output
0
Input
5
01010
Output
1
Input
8
11101111
Output
6
Note
In the first sample test it is possible to change the string like the following: .

In the second sample test it is possible to change the string like the following: .

In the third sample test it is possible to change the string like the following: .
我的想法是直接在输入的时候解决问题,输入的后一个数若与前一个数不同,则同时删除这两个数。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char a[200005];
int n,r=1;
int main()
{
    cin>>n;
    cin>>a[0];
    n--;
    while(n)
    {
        cin>>a[r];
        if(r==0)
        {
            r++;
            n--;
            continue;
        }
        if(a[r]!=a[r-1]) r--;
        else r++;
        n--;
    }
    cout<<r;
    return 0;
}

4.Number of Ways

添加链接描述](http://codeforces.com/problemset/problem/466/C)
You’ve got array a[1], a[2], …, a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .

Input
The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], …, a[n] (|a[i]| ≤  109) — the elements of array a.

Output
Print a single integer — the number of ways to split the array into three parts with the same sum.

Examples
Input
5
1 2 3 0 3
Output
2
Input
4
0 1 -1 0
Output
1
Input
2
4 1
Output
0
我觉得本题实质就是看把总和除3后,观察第一个sum/3与第二个sum/3之间0的个数及第二个sum/3与最后一个sum/3之间零的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 500005
using namespace std;
long long i,j,k,ans,s,n,sum,step[N];
int main()
{
    cin>>n;
    ans=0;
    sum=0;
    memset(step,0,sizeof(step));
    for(i=1;i<=n;i++)
    {
        cin>>s;
        step[i]=step[i-1]+s;
    }
    if(step[n]%3==0&&n>=3)
    {
        for(i=1;i<n;i++)
        {
            if(step[i]==2*step[n]/3)
                sum+=ans;
            if(step[i]==step[n]/3)
                ans++;
        }
    }
    cout<<sum<<endl;
    return 0;
}

5.DZY Loves Sequences
添加链接描述
DZY has a sequence a, consisting of n integers.

We’ll call a sequence ai, ai + 1, …, aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.

Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.

You only need to output the length of the subsegment you find.

Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109).

Output
In a single line print the answer to the problem — the maximum length of the required subsegment.

Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
此题我想的比较复杂,先从前往后遍历一遍找出最长序列,再从后往前遍历,找出二者中最优解。

#include<iostream>
#include<cstring>
#define N 100000+5
using namespace std;
typedef long long ll;
ll a[N],n,ans=1,index,Max,temp;
bool flag=true;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<n;)
    {
        if(a[i+1]>a[i])
        {
            ans++;
            i++;
            continue;
        }
        else if(flag)
        {
            temp=a[i+1];
            a[i+1]=a[i]+1;
            flag=false;
            index=i+1;
            ans++;
            i++;
            continue;
        }
        else if(!flag)
        {
            if(ans>Max) Max=ans;
            a[index]=temp;
            i=index;
            ans=1;
            flag=true;
            continue;
        }
    }
    if(ans>Max) Max=ans;
    ans=1;
    for(int i=n;i>1;)
    {
        if(a[i-1]<a[i])
        {
            ans++;
            i--;
            continue;
        }
        else if(flag)
        {
            temp=a[i-1];
            a[i-1]=a[i]-1;
            flag=false;
            index=i-1;
            ans++;
            i--;
            continue;
        }
        else if(!flag)
        {
            if(ans>Max) Max=ans;
            a[index]=temp;
            i=index;
            ans=1;
            flag=true;
            continue;
        }
    }
    if(ans>Max) Max=ans;
    cout<<Max;
    return 0;
}

6.Star sky

添加链接描述

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c).

Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 ≤ c, and 0, otherwise.

You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right — (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.

A star lies in a rectangle if it lies on its border or lies strictly inside it.

Input
The first line contains three integers n, q, c (1 ≤ n, q ≤ 105, 1 ≤ c ≤ 10) — the number of the stars, the number of the views and the maximum brightness of the stars.

The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 ≤ xi, yi ≤ 100, 0 ≤ si ≤ c ≤ 10) — the coordinates of i-th star and its initial brightness.

The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 ≤ ti ≤ 109, 1 ≤ x1i < x2i ≤ 100, 1 ≤ y1i < y2i ≤ 100) — the moment of the i-th view and the coordinates of the viewed rectangle.

Output
For each view print the total brightness of the viewed stars.

Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let’s consider the first example.

At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.

At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.

At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
我的思路比较简单,判断星星是否在范围内,若在范围内,则用

if(s[j]+t<=c) sum+=s[j]+t;
              
   else sum+=(s[j]+t)%(c+1);

来计算其亮度,但是超时。
超时代码

#include<iostream>
#include<cmath>
#define N 100005
using namespace std;
long long i,j,n,q,c,x[N],y[N],s[N],t,a,b,e,f,sum;
int main()
{
    cin>>n>>q>>c;
    for(i=1;i<=n;i++)
        cin>>x[i]>>y[i]>>s[i];
    for(i=1;i<=q;i++)
    {
        sum=0;
        cin>>t>>a>>b>>e>>f;
        for(j=1;j<=n;j++)
            if(x[j]>=a&&x[j]<=e&&y[j]>=b&&y[j]<=f)
            {
                if(s[j]+t<=c) sum+=s[j]+t;
                else sum+=(s[j]+t)%(c+1);
            }
        cout<<sum<<endl;
    }
    return 0;
}

AC代码


#include<iostream>
#include<cmath>
#define N 100005
using namespace std;
long long n,q,c,x,y,s,a[105][105][15],t,sum,dp[15][105][105];
int x1,y1,x2,y2;
int main()
{
    cin>>n>>q>>c;
     for(int i=0;i<n;i++)
        {
            cin>>x>>y>>s;
            a[x][y][s]++;
        }
        for(int k=0;k<=10;k++)
          for(int i=1;i<=100;i++)
            for(int j=1;j<=100;j++)
              dp[k][i][j]=dp[k][i][j-1]+a[i][j][k];
        int t,x1,x2,y1,y2;
        for(int i=0;i<q;i++)
        {
            sum=0;
            cin>>t>>x1>>y1>>x2>>y2;
            for(int k=0;k<=10;k++)
              for(int j=x1;j<=x2;j++)
                sum+=(dp[k][j][y2]-dp[k][j][y1-1])*((k+t)%(c+1));
            cout<<sum<<endl;
        }
    return 0;
}

7.Reports
添加链接描述
One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp’s joke to their news feed. Some of them reposted the reposts and so on.

These events are given as a sequence of strings “name1 reposted name2”, where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string “name1 reposted name2” user “name1” didn’t have the joke in his feed yet, and “name2” already had it in his feed by the moment of repost. Polycarp was registered as “Polycarp” and initially the joke was only in his feed.

Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp’s joke.

Input
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as “name1 reposted name2”. All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.

We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Output
Print a single integer — the maximum length of a repost chain.

Examples
Input
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya
Output
6
Input
6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp
Output
2
Input
1
SoMeStRaNgEgUe reposted PoLyCaRp
Output
2
此题我刚开始毫无头绪,后来在网上搜了一下,大神的思路是先把所有字母都转化为小写字母,再通过深搜即可解决。(侵删)

#include<iostream>
#include<string>
using namespace std;
int shu[201],n;
string a[201],b,c[201];
int dfs(string ok,int len,int st)
{
    int l=len;
    for(int i=st+1;i<=n;i++)
    {
        if(c[i]==ok)
        {
            l=max(l,dfs(a[i],len+1,i));//取最大值
        }
    }
    return l;
}
int main()
{
    int ans=1;
    cin>>n;
    string t="polycarp";
    int l=1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        for(int j=0;j<a[i].size();j++)
            if(a[i][j]>='A'&&a[i][j]<='Z') a[i][j]+=32;
        cin>>b;
        cin>>c[i];
        for(int j=0;j<c[i].size();j++)
            if(c[i][j]>='A'&&c[i][j]<='Z') c[i][j]+=32;
        if(c[i]==t) {
            shu[l++]=i;
        }
    }
    int len;
    string ok;
    for(int i=1;i<=l-1;i++)
    {
        len=2;
        ok=a[shu[i]];
        ans=max(dfs(ok,len,shu[i]),ans);//取最大值
    }
    cout<<ans;
    return 0;
}
————————————————
版权声明:本文为CSDN博主「ナナ色のブランク」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/amazingee/article/details/104749287/
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值