2021牛客暑期多校训练营2

C题 Draw Grids

ZYT and LBC decided to play a game.

They will alternately do the following operation.

  • Pick 4 integers a , b , c , d a,b,c,d a,b,c,d( 1 ≤ a 1\leq a 1a, c ≤ n c \leq n cn and 1 ≤ b 1\leq b 1b, d ≤ m d \leq m dm) , where ∣ a − c ∣ + ∣ b − d ∣ = 1 |a-c|+|b-d|=1 ac+bd=1 , and draw a straight segment between point ( a , b ) (a,b) (a,b) and point ( c , d ) (c,d) (c,d). The segment shouldn’t overlap with a segment that already exists.

  • In any time, the segments shouldn’t form a polygon. That is, there exists no sequence of distinct points ( x 0 , y 0 ) (x_0,y_0) (x0,y0), ( x 1 , y 1 ) (x_1,y_1) (x1,y1),⋯, ( x k − 1 , y k − 1 ) (x_{k−1},y_{k−1}) (xk1,yk1) satisfying for each integer 0 ≤ u < k 0 \leq u < k 0u<k,there is a segment between ( x u , y u ) (x_u,y_u) (xu,yu) and ( x ( u + 1 )  mod  k , y ( u + 1 )  mod  k ) (x_{ (u+1)\ \text{mod}\ k },y_{ (u+1)\ \text{mod}\ k }) (x(u+1) mod k,y(u+1) mod k).

The player who can’t perform an operation during his move loses.

Determine whether ZYT can win considering that both players play optimally and ZYT starts.
在这里插入图片描述
在这里插入图片描述

输入描述:

The input consists of one line containing two integers n {n} n and m {m} m.

输出描述:

Print “YES”(without quote) if ZYT can win and “NO”(without quote) if ZYT can’t.

示例1

输入

1 3

输出

NO

示例2

输入

2 2

输出

YES

备注:

It is guaranteed that 1 ≤ n , m ≤ 4 1\le n,m\le 4 1n,m4.

思路

不可以成为多边形,即不可以有环,必须是树形结构。 n × m n\times m n×m个顶点的生成树有 n × m − 1 n\times m-1 n×m1条边。

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    int x=n*m-1;
    puts(x&1?"YES":"NO");
}

D题 Er Ba Game

Er Ba Game is a popular card game in China’s Province Zhejiang.

In this problem, the game involves two players and 36 cards − 2 , 3 , ⋯   , 10 - 2,3,\cdots, 10 2,3,,10, each with four.

In the game each player takes 2 cards ( a 1 , b 1 ) (a_1,b_1) (a1,b1) , ( a 2 , b 2 ) (a_2,b_2) (a2,b2) .Player i {i} i gets ( a i , b i ) (a_i,b_i) (ai,bi) . Suppose a 1 ≤ b 1 a_1\leq b_1 a1b1, a 2 ≤ b 2 a_2\leq b_2 a2b2 , and we determine the winner as follow:

  1. ( 2 , 8 ) {(2,8)} (2,8) is the biggest pair.
  2. If neither is ( 2 , 8 ) {(2,8)} (2,8), a pair with a = b {a=b} a=b is greater than a pair without.
  3. If a 1 = b 1 a_1=b_1 a1=b1 and a 2 = b 2 a_2=b_2 a2=b2 , then compare a 1 a_1 a1, a 2 a_2 a2 .The bigger one wins.
  4. If a 1 ≠ b 1 a_1\ne b_1 a1=b1, a 2 ≠ b 2 a_2\ne b_2 a2=b2 , compare ( a 1 + b 1 ) mod  10 (a_1+b_1) \text{mod}\ 10 (a1+b1)mod 10, ( a 2 + b 2 )  mod  10 (a_2+b_2)\ \text{mod}\ 10 (a2+b2) mod 10.The bigger one wins.
  5. If a 1 ≠ b 1 a_1\ne b_1 a1=b1, a 2 ≠ b 2 a_2\ne b_2 a2=b2 and ( a 1 + b 1 )  mod  10 = ( a 2 + b 2 )  mod  10 (a_1+b_1)\ \text{mod}\ 10=(a_2+b_2)\ \text{mod}\ 10 (a1+b1) mod 10=(a2+b2) mod 10,compare b 1 , b 2 b_1,b_2 b1,b2 .The bigger one wins.

If 1 ≤ b 1 , a 2 ≤ b 2 _1\leq b_1,a_2\leq b_2 1b1,a2b2 does not hold, we can exchange a 1 a_1 a1 and b 1 b_1 b1 or exchange a 2 a_2 a2 and b 2 b_2 b2 until the formula holds.

You’re told a 1 a_1 a1, b 1 b_1 b1, a 2 a_2 a2, b 2 b_2 b2 , and you should tell who’s gonna win the game.

输入描述:

The first line contains an integer T {T} T — number of game cases.
Then T {T} T lines, each contains four integers a 1 a_1 a1, b 1 b_1 b1, a 2 a_2 a2, b 2 b_2 b2 , denoting the cards of each player.

输出描述:

Output T {T} T lines.

If player 1 wins, output “first” (without quote).

If player 2 wins, output “second” (without quote).

Otherwise, output “tie” (without quote).

示例1

输入

5
2 8 4 6
6 6 6 7
4 5 5 5
6 3 9 10
7 2 2 7

输出

first
first
second
second
tie

说明

The answers to the first four sets of data are obtained through the first, second, second, and fifth rules respectively.

备注:

It’s guaranteed that T ≤ 100 T\leq 100 T100, 2 ≤ a 1 , b 1 , a 2 , b 2 ≤ 10 2\leq a_1,b_1,a_2,b_2\leq 10 2a1,b1,a2,b210.

It’s not guaranteed that a 1 ≤ b 1 , a 2 ≤ b 2 a_1\leq b_1,a_2\leq b_2 a1b1,a2b2 .

思路

直接特判。

#include<bits/stdc++.h>
using namespace std;
int T,a1,b1,a2,b2;

int main(){
    cin>>T;
    while(T--){
        cin>>a1>>b1>>a2>>b2;
        if(a1>b1)   swap(a1,b1);
        if(a2>b2)   swap(a2,b2);
        if(a1==2&&b1==8){
            if(a2==2&&b2==8){    puts("tie");continue;  }
            puts("first");continue;
        }
        if(a2==2&&b2==8){   puts("second");continue;    }
        if(a1==b1){
            if(a2==b2){  puts(a1==a2?"tie":(a1>a2?"first":"second"));continue;    }
            puts("first");continue;
        }
        if(a2==b2){ puts("second");continue; }
        if((a1+b1)%10==(a2+b2)%10){ puts(b1==b2?"tie":(b1>b2?"first":"second"));continue;   }
        puts((a1+b1)%10>(a2+b2)%10?"first":"second");
    }
}

其他做法

给每个 ( x , y ) (x,y) (x,y)一个得分,将两个人的得分进行比较。

#include<bits/stdc++.h>
using namespace std;

int get(int x,int y){
    if(x>y) swap(x,y);
    if(x==2&&y==8)  return 10000;
    if(x==y)    return 1000+x;
    return  (x+y)%10*100+y;
}

int main(){
    int T,a1,b1,a2,b2;
    cin>>T;
    while(T--){
        cin>>a1>>b1>>a2>>b2;
        int x=get(a1,b1),y=get(a2,b2);
        if(x==y)    puts("tie");
        else if(x>y)    puts("first");
        else    puts("second");
    }
}

K题 Stack

ZYT had a magic permutation a 1 , a 2 a_1,a_2 a1,a2, ⋯ \cdots , a n a_n an , and he constructed a sequence b 1 b_1 b1, b 2 b_2 b2, ⋯ \cdots b n b_n bn by the following pseudo code:

Stk is an empty stack
for i = 1 to n :
    while ( Stk is not empty ) and ( Stk's top > a[i] ) : 
        pop Stk
    push a[i]
    b[i]=Stk's size

But he somehow forgot the permutation {a}a, and only got some k {k} k elements of b i b_i bi .

Construct a permutation a {a} a satisfying these b i b_i bi , or determine no such permutation exists.

Here a permutation refers to a sequence that contains all integers from 1 {1} 1 to n {n} n exactly once.

输入描述:

The first line contains two integers n {n} n, k {k} k ( 1 ≤ k ≤ n ) (1\leq k\leq n) (1kn) — the length of the permutation, the number of left b i b_i bi .

Then k {k} k lines each contains two integer p i p_i pi, x i x_i xi , denoting that b p i = x i b_{p_i}=x_i bpi=xi .

输出描述:

Output one line with n integers a 1 a_1 a1, a 2 a_2 a2, ⋯ \cdots a n a_n an — a possible permutation.

If no such permutation exists, output one integer -1.

示例1

输入

5 2
2 2
5 4

输出

1 2 5 3 4

示例2

输入

10 1
1 10

输出

-1

备注:

It’s guaranteed that n ≤ 1 0 6 n\leq 10^6 n106, 1 ≤ p i , x i ≤ n 1\leq p_i,x_i\leq n 1pi,xin, and ∀ i ≠ j , p i ≠ p j \forall i\ne j,p_i\ne p_j i=j,pi=pj .

思路

一个1到n的序列,存入单调栈中,问是否能在几个时刻使得栈的大小满足要求,如果能,构造一个这样的序列。

关键在于,快速找到第k个未使用过的数字,这里用了树状数组+二分来实现。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int c[maxn],a[maxn],b[maxn],n,k,p,v;

int ask(int x){
    int ret=0;
    for(;x;x-=x&-x) ret+=c[x];
    return ret;
}

void add(int x){
    for(;x<maxn;x+=x&-x)    c[x]--;
}

int get(int x){
    if(x<=0)    return -1;
    int l=1,r=n+1;
    while(l<r){
        int mid=(l+r)/2;
        if(ask(mid)>=x)   r=mid;
        else    l=mid+1;
    }
    if(l==n+1)  return -1;
    else    return l;
}

int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)   c[i]=(i&-i);
    while(k--){
        scanf("%d%d",&p,&v);
        a[p]=v;
    }
    int cur=1;
    for(int i=1;i<=n;i++){
        if(a[i]==0) a[i]=cur;
        else{
            if(a[i]>cur){   puts("-1");return 0;    }
            cur=a[i];
        }
        cur++;
    }
    for(int i=n;i>=1;i--){
        b[i]=get(a[i]);
        add(b[i]);
    }
    for(int i=1;i<=n;i++)   
        printf("%d%c",b[i]," \n"[i==n]);
}

其他做法

由于题目的特点,用前缀和来计算第k小数的位置。

#include<cstdio>
using namespace std;
const int maxn=1e6+10;
int n,m,p,v,a[maxn],b[maxn],cur,cnt[maxn];

int main(){
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d",&p,&v);
        b[p]=v;
    }
    for(int i=1;i<=n;i++){
        cur++;
        if(b[i]==0) b[i]=cur;
        else{
            if(b[i]>cur){puts("-1");return 0;}
            else    cur=b[i];
        }
    }
    for(int i=1;i<=n;i++)   cnt[b[i]]++;
    for(int i=2;i<=n;i++)   cnt[i]=cnt[i-1]+cnt[i];
    for(int i=1;i<=n;i++)   a[i]=cnt[b[i]]--;
    for(int i=1;i<=n;i++)   printf("%d%c",a[i]," \n"[i==n]);
}

I题 Penguins

Lovely penguins is a tiny game in which the player controls two penguins.

在这里插入图片描述

The game holds in two 20 × 20 20 \times 20 20×20 grids (the left one and the right one), and each has some blocked places.

We number the grid in x t h x^{th} xth row (starting from the up), y t h y^{th} yth column (starting from the left) ( x , y ) {(x,y)} (x,y).

The penguins move in four directions: up, down, left, right, exactly one step once. If its way is blocked, or it reaches the border, then this movement is omitted.

The player also moves the penguins in four directions, but the behavior of the two penguins is mirrored:

  1. L : left penguin moves to left, right penguin moves to right.
  2. R : left penguin moves to right, right penguin moves to left.
  3. U : both move upwards.
  4. D : both move downwards.

An operation can be omitted on one penguin but works on another.

The left penguin starts from ( 20 , 20 ) {(20,20)} (20,20) and wants to move to ( 1 , 20 ) {(1,20)} (1,20). The right penguin starts from ( 20 , 1 ) {(20,1)} (20,1) and wants to move to ( 1 , 1 ) {(1,1)} (1,1).If both penguin reach there destination, thay win the game.

Find out the shortest way to win the game.If there are many shortest ways to win, find the one with minimum lexicographical order(D<L<R<U).

Note: When one penguin reaches the destination and the other does not, the penguin that has reached the destination may still move out of the destination.

输入描述:

The input consists of 20 lines, each line contains 41 characters, describing the grids, separated by a space.

‘.’ means the grid is empty.

‘#’ means the grid is blocked.

输出描述:

Output 22 lines.

The first line contains the least number of steps to win the game.

The second line contains a string consisting of ‘L’,‘R’,‘U’,‘D’, describing a way to win.

There may be many ways to win, output the one with minimum lexicographical order.

Then output 20 lines, describing the track of the two penguins, mark the track with character ‘A’, and print as input.

示例1

输入

#................... .............##...#.
.................... .......#.....#.....#
.........#...#.#.... ...#....#...........
#........#.......... ...#..#.............
........#......#.... ..#.#......#.#.....#
......#.#..#.#....#. .......##.....##...#
....#...........#..# ....................
.##................. ...........#..#...#.
.....#.#........#.#. #.........#.#.......
.................... ..#....#..........#.
....#.#..........#.. .#.........#..#..#..
.........#.......#.. ..#.................
...#..#......#...#.. ......#.............
...........#...#.... ....................
..##..#.#....#..#... ..............#...#.
.#..#...#.#.....##.. .........#.#...#....
.#.........#........ ..............#.#...
..##.#........#...#. ##..................
....##.#............ .......#.....#......
..........##........ .#..#.#...........#.

输出

27
LULLUURRUUUUUUULUUUUURRUUUU
#..................A A............##...#.
...................A A......#.....#.....#
.........#...#.#...A A..#....#...........
#........#.........A A..#..#.............
........#......#.AAA AA#.#......#.#.....#
......#.#..#.#...A#. .A.....##.....##...#
....#...........#A.# .A..................
.##..............A.. .A.........#..#...#.
.....#.#........#A#. #A........#.#.......
.................AA. AA#....#..........#.
....#.#..........#A. A#.........#..#..#..
.........#.......#A. A.#.................
...#..#......#...#A. A.....#.............
...........#...#..A. A...................
..##..#.#....#..#.A. A.............#...#.
.#..#...#.#.....##A. A........#.#...#....
.#.........#....AAA. AAA...........#.#...
..##.#........#.A.#. ##A.................
....##.#........AAA. AAA....#.....#......
..........##......AA A#..#.#...........#.

备注:

It’s guaranteed that there always exists a way to win, and the penguins’ initial position is not blocked.

思路

四元组 { x 1 , y 1 , x 2 , y 2 } \{x1,y1,x2,y2\} {x1,y1,x2,y2}表示状态,进行广搜。另开一个 g [ N ] [ N ] [ N ] [ N ] g[N][N][N][N] g[N][N][N][N]数组,记录每个状态的前驱状态,用 h a s h hash hash的思想将一个四元组转换为一个 i n t int int值,存入 g g g数组中。

#include<bits/stdc++.h>
using namespace std;
constexpr int N=20;
constexpr int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
string a[N],b[N],path;
int f[N][N][N][N],g[N][N][N][N];
queue<tuple<int,int,int,int>> que;

int mirror(int x){
    if(x==1||x==2)  return x^3;
    else    return x;
}

pair<int,int> move(int x,int y,int i,int m){
    x+=dir[i][0],y+=dir[i][1];
    if(m==0&&(x<0||y<0||x>=N||y>=N||a[x][y]=='#'))
        x-=dir[i][0],y-=dir[i][1];
    if(m==1&&(x<0||y<0||x>=N||y>=N||b[x][y]=='#'))
        x-=dir[i][0],y-=dir[i][1];
    return {x,y};
}

int main(){
    ios::sync_with_stdio(false);
    for(int i=0;i<N;i++)    cin>>a[i]>>b[i];
    que.emplace(N-1,N-1,N-1,0);   f[N-1][N-1][N-1][0]=1;
    while(que.size()){
        auto [x1,y1,x2,y2]=que.front(); que.pop();
        for(int i=0;i<4;i++){
            auto [nx1,ny1]=move(x1,y1,i,0);
            auto [nx2,ny2]=move(x2,y2,mirror(i),1);
            if(f[nx1][ny1][nx2][ny2]==0){
                f[nx1][ny1][nx2][ny2]=f[x1][y1][x2][y2]+1;
                g[nx1][ny1][nx2][ny2]=((((x1*N+y1)*N)+x2)*N+y2)*4+i;
                que.emplace(nx1,ny1,nx2,ny2);
            }
        }
    }
    int x1=0,y1=N-1,x2=0,y2=0;
    cout<<f[x1][y1][x2][y2]-1<<endl;
    while(true){
        a[x1][y1]=b[x2][y2]='A';
        if(f[x1][y1][x2][y2]==1)    break;
        int G=g[x1][y1][x2][y2];
        path+="DLRU"[G%4],G/=4;
        y2=G%N;    G/=N;
        x2=G%N;    G/=N;
        y1=G%N;    G/=N;
        x1=G%N;
    }
    reverse(path.begin(),path.end());
    cout<<path<<endl;
    for(int i=0;i<N;i++)    cout<<a[i]<<" "<<b[i]<<endl;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值