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;
}
内容概要:本文介绍了一个针对电力系统连锁故障传播路径的N-k多阶段双层优化及故障场景筛选模型,该模型基于混合整数线性规划(MILP)方法构建,旨在全面评估电力系统在遭受多重故障时的脆弱性与恢复能力。通过引入故障传播路径的概念,模型能够动态模拟故障在电网中的逐级扩散过程,并结合多阶段优化策略,实现对关键故障场景的有效识别与优先排序。整个框架不仅考虑了初始故障元件的选取,还涵盖了后续因潮流转移引发的级联跳闸行为,从而提升了风险评估的准确性与时效性。该研究已在Matlab平台上完成代码实现,具备良好的可复现性和工程应用价值,适用于提升现代电网的安全防御水平。; 适合人群:电力系统、能源安全及相关领域的科研人员、高校研究生以及从事电网规划与运行管理的工程技术人员。; 使用场景及目标:①用于电力系统安全评估中识别最危险的N-k故障组合;②支撑电网应急预案制定与薄弱环节改造;③作为学术研究中关于级联故障建模与优化求解的教学与验证工具;④服务于智能电网背景下抵御蓄意攻击或极端事件的风险防控决策。; 阅读建议:建议读者结合Matlab代码深入理解模型的数学 formulation 与求解流程,重点关注目标函数设计、约束条件构建及双层优化结构的实现逻辑,同时可通过调整系统参数和故障设定进行仿真对比分析,以掌握不同因素对连锁故障演化的影响规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值