1 如此编码
100
#include<iostream>
using namespace std;
int main()
{
int n, m;
int a[25], b[25], c[25];
cin >> n >> m;
int i, j;
c[0] = 1;
for (i = 1; i <= n; i++)
{
cin >> a[i];
c[i] = a[i] * c[i - 1];
}
int cur, last = 0, temp;
for (i = 1; i <= n; i++)
{
cur = m % c[i];
temp = cur;
cur -= last;
last = temp;
b[i] = cur / c[i - 1];
if (i == 1)cout << b[i];
else cout << " " << b[i];
}
return 0;
}
2 何以包邮?
70分
#include<iostream>
using namespace std;
int n, m, op[35] = { 0 }, a[35];
int ans = 300005;
void f(int x)
{
if (x == n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
if (op[i])sum += a[i];
}
if (sum < m)return;
else
{
if (sum < ans)ans = sum;
}
return;
}
op[x] = 1;
f(x + 1);
op[x] = 0;
f(x + 1);
}
int main()
{
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)
cin >> a[i];
f(0);
cout << ans << endl;
return 0;
}
参考https://blog.csdn.net/qq_62108340/article/details/127417283 Greater_WZH 评论对其进行剪枝。即向下递归时,默认后续节点均被选择,计算sum如果小于包邮费用,则不再向下递归。
100分
#include<iostream>
using namespace std;
int n, m, op[35] = { 0 }, a[35];
int ans = 300005;
void f(int x,int sum)
{
if (x == n)
{
if (sum >= m && sum < ans)ans = sum;
return;
}
int temp, i;
op[x] = 1;
temp = sum + a[x];
for (i = x + 1; i < n; i++)
temp += a[i];
if (temp < m)return;
f(x + 1, sum + a[x]);
op[x] = 0;
temp = sum;
for (i = x + 1; i < n; i++)
temp += a[i];
if (temp < m)return;
f(x + 1, sum);
}
int main()
{
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)
cin >> a[i];
f(0,0);
cout << ans << endl;
return 0;
}
动态规划参照网上多篇博客,以背包问题的思想解决。本题要满足的是超过某一阈值的最小值,01背包问题是不超过某一阈值,从数轴上看,前者向左靠近阈值;后者向右靠近阈值,取反即可使得二者方向相同。
可行的做法是选中所有物品,减去包邮的最低费用m,得到一个定值sum。原问题旨在给出一个方案,使得其超过m最少,可以认为占据sum最少,取反就是如何占据这个sum最多,即背包问题,如何利用空间最多。
100分
#include<iostream>
using namespace std;
int n, m, a[35];
int dp[35][300005] = { 0 };
int max(int a, int b)
{
if (a > b)return a;
return b;
}
int main()
{
cin >> n >> m;
int i, j, sum = 0, t1, t2;
for (i = 0; i < n; i++)
{
cin >> a[i];
sum += a[i];
}
sum -= m;
for (i = 0; i < n; i++)
{
if (i == 0 )
{
for (j = 0; j <= sum; j++)
{
if (j >= a[i])dp[i][j] = a[i];
}
continue;
}
for (j = 0; j <= sum; j++)
{
if (j >= a[i])dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - a[i]] + a[i]);
else dp[i][j] = dp[i - 1][j];
}
}
cout << sum + m - dp[n - 1][sum] << endl;
return 0;
}
3 防疫大数据
40
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
struct data_item
{
int d, u, r, receive_d;
data_item(int a, int b, int c, int d)
{
d = a;
u = b;
r = c;
receive_d = d;
}
data_item(){}
};
struct danger_region
{
int r;
int start;
danger_region(int a, int b)
{
r = a;
start = b;
}
};
vector<data_item> history_data;
vector<data_item> last_7d_data;
vector<danger_region> cur_danger_region;
set<int> myset;
priority_queue<int,vector<int>,greater<int>> target_queue;
int main()
{
int n;
int r, m, danger_r;
int i, j;
int flag;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> r >> m;
for (j = 0; j < r; j++)
{
cin >> danger_r;
cur_danger_region.push_back(danger_region(danger_r, i));
}
for (j = 0; j < m; j++)
{
data_item data;
cin >> data.d >> data.u >> data.r;
data.receive_d = i;
//last_7d_data.push_back(data);
history_data.push_back(data);
}
myset.clear();
for (auto jj : history_data)
{
for (auto ii : cur_danger_region)
{
if (jj.receive_d > i-7 && jj.receive_d <= i && jj.d > i - 7 && jj.d <= i)
{
if (jj.r == ii.r && jj.d >= ii.start && jj.d < ii.start + 7)
{
if (ii.start <= jj.d && i <= ii.start + 6)
myset.insert(jj.u);
}
}
}
}
cout << i;
for (auto cur : myset)
{
target_queue.push(cur);
}
while (!target_queue.empty())
{
cout << " " << target_queue.top();
target_queue.pop();
}
cout << endl;
}
return 0;
}
4 吉祥物投票
20
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int a[10000000] = { 0 }, b[100005] = { 0 };
int main()
{
int n, m, q;
int i, j;
int op, l, r, x, y, w;
cin >> n >> m >> q;
for (i = 0; i < q; i++)
{
cin >> op;
if (op == 1)
{
cin >> l >> r >> x;
for (j = l ; j <= r; j++)
{
if (a[j])
{
b[a[j]]--;
}
a[j] = x;
b[x]++;
}
}
else if (op == 2)
{
cin >> x >> w;
for (j = 1; j <= n; j++)
{
if (a[j] == x)a[j] = w;
}
if (w)b[w] += b[x];
b[x] = 0;
}
else if (op == 3)
{
cin >> x >> y;
for (j = 1; j <= n; j++)
{
if (a[j] == x)a[j] = y;
else if (a[j] == y)a[j] = x;
}
int temp = b[x];
b[x] = b[y];
b[y] = temp;
}
else if (op == 4)
{
cin >> w;
if (w)
{
cout << b[w] << endl;
}
else
{
int num = 0;
for (j = 1; j <= n; j++)
{
if (a[j] == 0)num++;
}
cout << num << endl;
}
}
else if (op == 5)
{
int mm = -1, index = -1;
for (j = 1; j <= m; j++)
{
if (b[j] > mm)
{
mm = b[j];
index = j;
}
}
if (mm > 0)cout << index << endl;
else cout << 0 << endl;
}
}
return 0;
}