AtCoder C - Jumping Takahashi

这篇博客介绍了一道编程题目,涉及C++实现,通过动态规划解决给定数组a和b,判断是否存在一种连续选择方式,使得最终序列和不超过给定限制x。核心代码展示了如何使用递推和更新状态来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

题目链接

C++代码

#include<bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i ++)
#define REP(i, a, b) for(int i = a; i <= b; i ++)
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x).size())

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
const int N = 110;
const int M = 11000;
bool dp[N][M];
int a[N], b[N];

LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL Pow(LL a, LL k){LL res = 1; while(k){if(k & 1) res *= a; k >>= 1; a *= a;} return res;}

mt19937 rnd(random_device{}());
int mrand(int x){return rnd() % x;}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	dp[0][0] = 1;
	int n, x;
	cin >> n >> x;
	
	REP(i, 1, n) cin >> a[i] >> b[i];
	
	REP(i, 0, n)
		REP(j, 0, x){
		
			if(dp[i][j]){
				if(a[i + 1] + j <= x) dp[i + 1][a[i + 1] + j] = true;
				if(b[i + 1] + j <= x) dp[i + 1][b[i + 1] + j] = true;
			}
		}
	
	if(dp[n][x]) cout << "Yes" << '\n';
	else cout << "No" << '\n';
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值