Sicily 1877. Construction

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
8

9

// 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;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值