1877. Construction
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
0-1 string comes again.0-1 string is a string which is composed of zeros and ones only .Your task is very simple. Just construct a 0-1 string according to the given demand.
Input
Input consists of several test cases.
Each case begins with an integer N, (1<=N<=5000), the length of the string you have to construct, then follows an integer M, (0<=M<=3000), the number of demands, then next M lines come, with three integers B , E , T per line (1<= B <= E <=N , 0 <=T<= E-B+1).For each line, make sure that the string you construct has at least T ones from position B to position E.(The first position is numbered 1, and the last position N).
You should construct a 0-1 string using the least ones to satisfy all the demands.
Input is ended by EOF.
Output
For each test case, output an integer indicating how many one is in your string. Then output the position of number one in ascending order.
Sample Input
9 4 1 4 2 4 6 2 8 9 2 3 5 2
Sample Output
5 1 4 5 89
// Problem#: 1877 // Submission#: 3590221 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <stdio.h> #include <algorithm> using namespace std; const int MAXM = 3010; const int MAXN = 5010; int n, m; struct node { int b, e, t; }; int arr[MAXN]; node demands[MAXM]; int sum[MAXN]; bool cmp(const node & n1, const node & n2) { if (n1.e != n2.e) return n1.e < n2.e; return n1.b < n2.b; } void solve() { int i, j; for (i = 0; i <= n; i++) arr[i] = sum[i] = 0; int planted = 0, y, tree, add; for (i = 0; i < m; i++) { y = demands[i].e; tree = sum[demands[i].b] - sum[y + 1]; add = 0; while (tree < demands[i].t) { while (arr[y]) sum[y--] += add; tree++; arr[y] = 1; planted++; add++; sum[y--] += add; } if (add) for (j = 1; j <= y; j++) sum[j] += add; } printf("%d\n", planted); for (i = 1; i <= n; i++) if (arr[i] == 1) printf("%d\n", i); } void process() { int i; for (i = 0; i < m; i++) scanf("%d%d%d", &demands[i].b, &demands[i].e, &demands[i].t); sort(demands, demands + m, cmp); solve(); } int main() { while (scanf("%d%d", &n, &m) != EOF) process(); return 0; }