2021牛客暑期多校训练营2 C\D\K

C Draw Grids

题意

两个人按顺序在点阵上画长度为1的线,不能斜着画。问当没有封闭图形的时候,最后一画是第一个人画的输出NO,否则输出YES

题解

我们画图可以发现,首先矩阵的转置答案肯定和转置前的答案是相同的,所以只需要考虑一半的答案。

那么我们考虑让每个人都尽可能的多画,肯定先画不垂直的线,也就是先画所有的横着或者竖着的线。画完之后,我们只能再画反方向(指的是另一个方向)的了。那么这时候显然只能画一溜,那么我们最后两个人一共画了n * m - 1的线。那么我们只需要判断这个数的奇偶即可。

Code

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int n, m;

int main(){
	cin >> n >> m;
    if((n * m - 1) % 2 == 0){ // 注意优先级!!!
        cout << "NO" << endl;
    }
    else{
        cout << "YES" << Endl;
    }
	re 0;
}

D Er Ba Game

题意

二八游戏,按题意给定的规则比较大小

题解

模拟,注意 if 的判断顺序

Code

#include <iostream>

#define x first
#define y second

using namespace std;

typedef pair<int, int>PII;

int main()
{
    int T;
    cin >> T;
    while(T -- )
    {
        PII a, b;
        cin >> a.x >> a.y >> b.x >> b.y;
        if(a.x > a.y){
        	swap(a.x, a.y);
        }
        if(b.x > b.y){
        	swap(b.x, b.y);
        }
        if (a.x == 2 && a.y == 8 && b.x == 2 && b.y == 8) cout<< "tie" << endl;
        else if(a.x == 2 && a.y == 8) cout << "first" << endl;
        else if(b.x == 2 && b.y == 8) cout << "second" << endl;
        
        else if(a.x == a.y && b.x == b.y) 
        {
            if(a.x > b.x) cout << "first" << endl;
            else if(a.x < b.x) cout << "second" << endl;
            else cout << "tie" << endl;
        }
        else if(a.x == a.y) cout << "first" << endl;
        else if(b.x == b.y) cout << "second" << endl;
        else 
        {
            if((a.x + a.y) % 10 > (b.x + b.y) % 10) cout << "first" << endl;
            else if((a.x + a.y) % 10 < (b.x + b.y) % 10) cout << "second" << endl;
            else
            {
                if(a.y == b.y) cout << "tie" << endl;
                else if(a.y > b.y) cout << "first" << endl;
                else cout << "second" << endl;
            }
        }
    }
    return 0;
    
}
	

K Stack

题意

题目的意思是我们有一个单调递增的单调栈,我们有1~n的全排列 n,存在 a[i] 中,按照全排列给定的顺序往栈里放数。b 数组存的是放入当前元素后栈中元素的个数。现在给定部分的b 数组,让你构造一个可能的 a 数组。

题解

这个题在 Leonard 巨巨的点拨下,本质这道题是“谜一样的牛”的变式

那么当给定我们部分b数组,我们是否可以根据部分 b 数组来构造可能能满足当前部分 b 数组的全部 b 数组呢?

首先显然,当 p > b[p] 的时候是不成立的,输出 -1。

我们考虑每个没给过的位置。

显然根据题意 b[1] = 1,这是无可厚非的。

我们考虑 b 的本质是什么。我们在做“谜一样的牛”的时候,每个给定的数说明的是当前牛的前面有几个牛比它矮的,那么就能说明这当前给定的这个牛一定是还没确定好的数中第 bi + 1 小的数。同理移植到这道题上来说,每个 b 表示的是这个数在剩下的没放完的数中第 b 小的数(这是根据题意来的)。所以我们得出来这样一个结论:我们不需要考虑每个数到底是几,只需要知道这个数是在当前没有选择的数中第几小的就可以了。

我们想,每个没给过的这个 b 对应的位置的数,从前往后考虑,能保证每个数的顺序大小能尽可能的先用上,保证后面的数的正确性。

我们刚才得出来结论,只需要考虑第几小的就可以了。怎么确定每个位置可以是多少呢?对于每个没有确定的数,我们可以令他为前一个数 +1 。这样我们可以优先利用小的,来保证后面的合法性。

// Problem: Stack
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11253/K
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Code by: ING__
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
#define re return
#define Endl "\n"
#define endl "\n"

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

const int N = 1e6 + 10;

int n, m;
int tr[N];
int ans[N];
int h[N];
bool vis[N];

int lb(int x){
    return x & -x;
}

void modify(int p, int v){
    for(; p <= n; p +=lb(p)){
        tr[p] += v;
	}
}

int query(int p){
    int res = 0;
    for(; p; p -= lb(p)){
		res += tr[p];
    }
    return res;
}

int main(){
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++){
		modify(i, 1); // 表示当前数还能用几次
	}
	
	int flag = 0;
	
	for(int i = 1; i <= m; i++){
		int p, b;
		scanf("%d%d", &p, &b); // 卡cin
		h[p] = b;
		if(b > p){
			flag = 1;
		}
	}
	
	if(flag){
		cout << -1;
		return 0;
	}
	
	h[1] = 1;
	for(int i = 2; i <= n; i++){
		if(!h[i]) h[i] = h[i - 1] + 1;
	}
	
	for(int i = n; i; i--){
		int k = h[i];
		int l = 1;
		int r = n;
		while(l < r){
			int mid = (l + r) / 2;
			if(query(mid) >= k){
				r = mid;
			}
			else l = mid + 1;
		}
		ans[i] = r;
		modify(r, -1);
	}
	
	for(int i = 1; i <= n; i++){
		printf("%d ", ans[i]);
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值