跳到正文

题解

题解:P16353 「Diligent-OI R3 A」说好不哭

原文
文章信息与版本

文章信息

收录
2026年6月6日
更新
2026年6月6日
篇幅
约 2 分钟
题目
P16353

版本

  1. v1当前

P16353 「Diligent-OI R3 A」说好不哭题解

题面:

小 C 想知道,是否存在一个长度为 nn 的整数序列满足最大非空子段和为 xx,最小非空子段和为 yy。

若存在,输出 YES,否则输出 NO。

请注意,若序列 bb 可以通过将序列 aa 分别在前面和后面删除若干个元素(可以为 00 个)得到,则定义 bb 是 aa 的子段。

思路:

喜欢不开 long long 的人有福了

考虑多种情况

  1. n=0n = 0 的情况,这种情况只有 x==yx == y 才可以

  2. y>0y > 0 的情况,只要 x>=n×yx >= n \times y 就可以,反之则不行

  3. x<0x <0 且不符合情况2的情况下,只要满足 y<=n×xy <= n \times x就可以 ,反之则不行

  4. 不符合以上情况的其他情况均可

思路有了,代码就很简单了:

code:

C++
#include<iostream>
#define ll long long

using namespace std;

ll t,n,x,y;

bool judge(){
	if(y > 0){
        if(x >= n * y)return true;
        else return false;
    }
	else if(x < 0){
        if(y <= n * x)return true;
		else return false;
    }
	else{
        return true;
    }
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cin.tie(0);
	cin>>t;
	for(int i = 1;i <= t; i ++){
		cin>>n>>x>>y;
		if(n==1){
			if(x == y)cout<<"YES\n";
			else cout<<"NO\n";
			continue;
		}
		if(judge() == false){
			cout<<"NO\n";
		}
		else{
			cout<<"YES\n";
		}
	}
	return 0;
} 
原文