codeforces 724

题目链接:

http://codeforces.com/contest/724

A. Checking the Calendar
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given names of two days of the week.

Please, determine whether it is possible that during some non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of the next month was equal to the second day of the week you are given. Both months should belong to one year.

In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.

Names of the days of the week are given with lowercase English letters: "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".

Input

The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".

Output

Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes).

Examples
input
monday
tuesday
output
NO
input
sunday
sunday
output
YES
input
saturday
tuesday
output
YES

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e5+10;
string str1,str2;
int check(string s)
{
    if(s=="monday")return 0;
    else if(s=="tuesday")return 1;
    else if(s=="wednesday")return 2;
    else if(s=="thursday")return 3;
    else if(s=="friday")return 4;
    else if(s=="saturday")return 5;
    else return 6;
}
int main()
{
    cin>>str1>>str2;
    int a=check(str1),b=check(str2);
    int t=(b-a+7)%7;
    if(28%7==t||30%7==t||31%7==t)printf("YES\n");
    else printf("NO\n");
    return 0;
}
/*

题意:

水;

思路:
水;
*/

  





B. Batch Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a table consisting of n rows and m columns.

Numbers in each row form a permutation of integers from 1 to m.

You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.

You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.

Each of next n lines contains m integers — elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.

Output

If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes).

Examples
input
2 4
1 3 2 4
1 3 4 2
output
YES
input
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
output
NO
input
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
output
YES
#include <bits/stdc++.h>
using namespace std;
int n,m,a[23][23],num[23],flag=1;
int check(int x,int y)
{
    for(int i=1;i<=n;i++)
    {
        int cnt=0;
        for(int j=1;j<=m;j++)
        {
            if(j==x)
            {
                if(a[i][j]!=y)cnt++;
            }
            else if(j==y)
            {
                if(a[i][j]!=x)cnt++;
            }
            else 
            {
                if(a[i][j]!=j)cnt++;
            }
        }
        if(cnt>2)return 0;
    }
    return 1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
    for(int i=1;i<=n;i++)
    {
        num[i]=0;
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]!=j)num[i]++;
        }
        if(num[i]>4)
        {
            cout<<"NO";
            return 0;
        }
        if(num[i]>2)flag=0;
    }
    if(flag)
    {
        cout<<"YES";
        return 0;
    }
    flag=0;
    for(int i=1;i<=m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            if(check(i,j))flag=1;
        }
    }
    if(flag)cout<<"YES";
    else cout<<"NO";
    return 0;
}
/*

题意:

每行交换最多一次,最后还可以交换两列,问最后每行是否都能变成1,2,...m的排列;

思路:

枚举最后要交换哪两列,然后判断每行是否能在一次变换中变成列交换之前的状态,还有判断不进行列交换的时候;
*/

  



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

There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.

Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.

At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.

When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.

For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print  - 1 for such sensors.

Input

The first line of the input contains three integers nm and k (2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.

Each of the following k lines contains two integers xi and yi (1 ≤ xi ≤ n - 1, 1 ≤ yi ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.

Output

Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or  - 1 if this will never happen.

Examples
input
3 3 4
1 1
1 2
2 1
2 2
output
1
-1
-1
2
input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
output
1
-1
-1
2
5
-1
input
7 4 5
1 3
2 2
5 1
5 3
4 3
output
13
2
9
5
-1


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf=1e18;
const int maxn=1e5+5;
int k,fx[maxn],fy[maxn];
LL dir[4][2]={-1,1,-1,-1,1,1,1,-1};
LL tx[4],ty[4],n,m;
LL gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}
LL GCD(LL a,LL b)
{
    if(!b)return a;
    return GCD(b,a%b);
}
LL solve(LL fn,LL fm)
{
    LL b=fm-fn;
    LL x,y,d;
    d=gcd(n,-m,x,y);
    if(b%d!=0)return -1;
    if(b%m==0)return fn;
    LL lcm=abs(n*m/d);
    x=x*(b/d);
    LL ans=((x*n+fn)%lcm+lcm)%lcm;
    if(ans==0)ans=lcm;
    return ans;
}
int main()
{
    scanf("%I64d%I64d%d",&n,&m,&k);
    for(int i=1;i<=k;i++)scanf("%d%d",&fx[i],&fy[i]);
    LL hi=n/GCD(n,m)*m;
    n*=2;m*=2;
    for(int i=1;i<=k;i++)
    {
        LL ans=inf;
        for(int j=0;j<4;j++)
        {
            LL temp=solve(fx[i]*dir[j][0],fy[i]*dir[j][1]);
            if(temp>0&&temp<=hi)ans=min(ans,temp);
        }
        if(ans==inf)printf("-1\n");
        else printf("%I64d\n",ans);
    }
    return 0;
}
/*
题意:
给出一束光从(0,0)出发,一直到角落才会停,问光到达给出的这些点的最短时间是多少;

思路:

可以发现这些点在平面上镜面对称后得到的点是(2*k*n+-x,2*t*m+-y);然后坐标相等,就是解同余方程了;
范围在(0,lcm(n,m))才是正确的;
*/

  

D. Dense Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s, consisting of lowercase English letters, and the integer m.

One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.

Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.

Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < ... < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j,  j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1.

Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt.

Find the lexicographically smallest string, that can be obtained using this procedure.

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 100 000).

The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.

Output

Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.

Examples
input
3
cbabc
output
a
input
2
abcab
output
aab
input
3
bcabcbaccba
output
aaabb

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int m;
char s[maxn],ans[maxn],tep[maxn];
int main()
{
    scanf("%d",&m);
    scanf("%s",s);
    int len=strlen(s),cnt,num;
    for(int i=len-1;i>=0;i--)s[i+1]=s[i];
    for(char temp='a';temp<='z';temp++)
    {
        int p=0,flag=1,fp=0;num=0;
        for(int i=1;i<=len;i++)
        {
           if(i<=p+m)
           {
                if(s[i]<temp)tep[num++]=s[i],p=i;
                else if(s[i]==temp)fp=i;
           }
           else 
           {
                if(fp>p)
                {
                    tep[num++]=s[fp],p=fp;i--;
                }
                else {flag=0;break;}
           }
        }
        if(flag)
        {
            for(int i=0;i<num;i++)ans[i]=tep[i];
            cnt=num;
            break;
        }
    }
    if(cnt==0)
    {
        char d='z';
        for(int i=1;i<=len;i++)d=min(d,s[i]);
        cnt=1;
        ans[0]=d;
    }
    sort(ans,ans+cnt);
    for(int i=0;i<cnt;i++)printf("%c",ans[i]);
    return 0;
}
/*
题意:
每隔m个字符选一个,使得选中的这些字符排序后字典序最小;

思路:

枚举最大的那个字符,遍历一遍,比其小的全要,相等的要当前范围最后一个;
*/

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5946232.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值