The question is hard to understand.
Given the railway capacity, number of stations, and several orders, it is required to determine how much profit is maximized.
I want to be greedy, but it cannot be determined that it is the optimal solution.
If you use dfs, select or do not select the number of current users for each site. If you want to select the number, add the number of users for each site to determine whether there will be superhumans, the next round of dfs will not be selected, and then the number of people will be reduced to enter the unselected dfs.
This question is said to have timed out when marking with arrays...
Code:
#include <cstdio> const int maxn = 30; int cap, num, ord, ans; int cnt[10]; struct Order { int s; int e; int p; }; Order o[maxn]; bool judge() { for (int i = 0; i < num; i++) if (cnt[i] > cap) return false; return true; } void dfs(int d, int sum) { if (sum > ans) ans = sum; if (d >= ord) return; for (int i = o[d].s; i < o[d].e; i++) cnt[i] += o[d].p; if (judge()) //choose dfs(d + 1, sum + o[d].p * (o[d].e - o[d].s)); for (int i = o[d].s; i < o[d].e; i++) cnt[i] -= o[d].p; dfs(d + 1, sum); //not choose } int main() { while (scanf("%d%d%d", &cap, &num, &ord) && (cap || num || ord)) { for (int i = 0; i < num; i++) cnt[i] = 0; for (int i = 0; i < ord; i++) scanf("%d%d%d", &o[i].s, &o[i].e, &o[i].p); if (!cap || !num || !ord) { printf("0\n"); continue; } ans = 0; dfs(0, 0); printf("%d\n", ans); } return 0; }