Sum of Three Terms(构造)

该博客展示了一段C++代码,用于解决一道数学问题。问题涉及寻找序列在特定条件下的极限解。代码通过迭代和边界条件判断,确定是否存在满足条件的序列。博客内容探讨了如何通过化简公式并找到极限条件来构建可行解,并提供了详细的解题思路。
摘要由CSDN通过智能技术生成
https://atcoder.jp/contests/arc135/tasks
#include<iostream>
#include<map>
#include<cstring>
using namespace std;

#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
	int x = 0, f = 0;
	char ch = getchar();
	while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f?-x:x;
}
template<typename T> void print(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x >= 10) print(x/10);
	putchar(x % 10 + '0');
}
const int mod = 998244353;
const int N = 3e5 + 10;

int a[N];
int s[N];
int n;
void solve() {
	cin >> n;
	for (int i = 0; i< n; i ++) cin >> a[i];
	
	int minvy = 0, minvx = 0;
	//负数不用考虑,因为他原本是正数,就一定满足大于等于0的条件,然后我们只要找到最小的负数就行了
	int maxx = 0x3f3f3f3f;
	
	for (int i = 2; i< n + 2; i ++) {
		s[i] = a[i - 2] - s[i - 1] - s[i - 2];
		if (i % 3 == 0) {
			minvy = max (minvy, -s[i]);
		} 
		else if (i % 3 == 1) {
			minvx = max(minvx, -s[i]);
		}
		else {
			maxx  = min(maxx, s[i]);
		}
	}
	if (minvy + minvx > maxx) {
		puts("No");
		return ;
	}
	puts("Yes");
	s[0] = minvy, s[1] = minvx;
	for (int i = 2; i < n + 2; i ++) 
		s[i] = a[i - 2] - s[i - 1] - s[i - 2];
	for (int i = 0; i < n + 2; i ++)
		cout << s[i] << " \n"[i == n + 1];
}
int main () {
    int t;
    t = 1;
    while (t --) solve();
    return 0;
}


这题通过找边界范围然后构造可行解的。 通过对公式化简 可以发现 i % 3 == 0 s[i] = x +a i % 3 == 1 s[i] = x + b i % 3== 2 s[i] = x - a - b 我们要找到是否可行的极限条件 为此我们构造出 c1, c2, c3 c1 <= a c2 <= b a + b <=c3 c1 + c2<= a + b <= c3 左边取最大,右边取最小然后判断是否满足大小关系判断是否可行。 如果满足直接把值代入就可以了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值