简单入门题
第一题
给出容量为N的背包、n个物品和c个体积为1的填充块,询问是否能够将背包刚好充满。
价值与体积相同的0/1背包,结尾判断差值是否小于c
#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N,n,c;
cin >> N >> n >> c;
vector<int> nums;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
nums.push_back(tmp);
}
vector<int> dp(N + 1, 0);
//dp[i]容量为i的箱子最多装多少
for (int i = 0; i < n; i++) {
for (int j = N; j >= nums[i]; j--) {
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);
}
}
if (N - dp[N] <= c) cout << "YES" << endl;
else cout << "NO"<< endl;
}
}
第二题
每个位置可以从两个值中选取一个,询问能否组成单调不增或单调不减队列
模拟抽取即可
#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> nums1(n);
vector<int> nums2(n);
for (int i = 0; i < n; i++) {
cin >> nums1[i];
}
for (int i = 0; i < n; i++) {
cin >> nums2[i];
}
//依次考虑升序或者降序
int flag = false;
int now = 0;
for (int i = 0; i < n; i++) {
int tmp1 = min(nums1[i], nums2[i]);
int tmp2 = max(nums1[i], nums2[i]);
if (tmp1 >= now) {
now = tmp1;
} else if (tmp2 < now) {
break;
} else now = tmp2;
if (i == n - 1) flag = true;
}
now = 10005;
for (int i = 0; i < n; i++) {
int tmp1 = min(nums1[i], nums2[i]);
int tmp2 = max(nums1[i], nums2[i]);
if (tmp2 <= now) {
now = tmp2;
} else if (tmp1 > now) {
break;
} else now = tmp1;
if (i == n - 1) flag = true;
}
if (flag) cout <<"YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
标签:int,9.14,笔试,cin,++,now,小米,nums1,dp
From: https://www.cnblogs.com/tanch25/p/18421377