Codeforces 361C - Levko and Array Recovery(贪心)

C. Levko and Array Recovery
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Levko loves array a1, a2, … , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:

Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri.
Find the maximum of elements from li to ri. That is, calculate the value这里写图片描述 .
Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn’t exceed 109 in their absolute value, so he asks you to find such an array.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko’s records, correspondingly.

Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers li, ri and di (1 ≤ li ≤ ri ≤ n,  - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers li, ri and mi (1 ≤ li ≤ ri ≤ n,  - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type.

The operations are given in the order Levko performed them on his array.

Output
In the first line print “YES” (without the quotes), if the solution exists and “NO” (without the quotes) otherwise.

If the solution exists, then on the second line print n integers a1, a2, … , an (|ai| ≤ 109) — the recovered array.

Examples
input
4 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 3 4 8
output
YES
4 7 4 7
input
4 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 3 4 13
output
NO

题意:
给出m个操作,如果操作数为1,那么区间内的数加上目的数.
如果操作数为2,那么要求区间内最大数为目的数.
要求构造出这么样的初始数列,如果不能,输出NO.

解题思路:
贪心求出当前位置的最小数.
然后再执行一遍过程,如果在操作数为2的情况下区间最大数不为目的数,那么数列不可构造.

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define INT_MAX 100000000
const int maxn = 5e3+5;
struct node
{
    int op;
    int l;
    int r;
    int d;
}cmd[maxn];;
int a[maxn];
int b[maxn];
int main()
{
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
        cin >> cmd[i].op >> cmd[i].l >> cmd[i].r >> cmd[i].d;
    for(int i = 1; i <= n; i++)
        a[i] = INT_MAX;
    for(int i = 1; i <= m; i++)
    {
        if(cmd[i].op == 1)
        {
            for(int j = cmd[i].l; j <= cmd[i].r; j++)
                b[j] += cmd[i].d;
        }
        else
        {
            for(int j = cmd[i].l; j <= cmd[i].r; j++)
            {
                a[j] = min(a[j], cmd[i].d - b[j]);
            }
        }
    }
    for(int i = 1; i <= n; i++)
        b[i] = a[i];
    for(int i = 1; i <= m; i++)
    {
        if(cmd[i].op == 1)
        {
            for(int j = cmd[i].l; j <= cmd[i].r; j++)
                b[j] += cmd[i].d;
        }
        else
        {
            int flag = INT_MIN;
            for(int j = cmd[i].l; j <= cmd[i].r; j++)
                flag = max(flag, b[j]);
            if(flag != cmd[i].d)
            {
                cout << "NO";
                return 0;
            }
        }
    }
    cout << "YES" << endl;
    for(int i = 1; i <= n; i++)
        cout << a[i] << " ";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值