A. Levko and Array Recovery----思维题
2017-08-07 12:58
375 查看
A. Levko and Array Recovery
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
output
input
output
题目链接:http://codeforces.com/contest/360/problem/A
题目的意思是说对于一个序列有两个操作:
1.(1,l,r,p)..将区间[l,r]所有数都加上p
2.(2,l,r,m).求出区间[l,r]的最大值为m
现在给你这个操作,问你时候能够找到原序列,任意输出一个正确答案即可。
这个题我没有想出来,看了大牛的题解。
这个题就是求出每个位置初始时可能的最小值,然后判断是否合法。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#define inf 1000000007
using namespace std;
struct node{
int t,l,r,m;
} op[5010];
int a[5010],b[5010],n,m;
bool solve(){
int p,i,k;
for (i=1;i<=n;i++){
a[i]=1000000000;
}
memset(b,0,sizeof(b));
for (p=1;p<=m;p++){
if (op[p].t==1)
for (i=op[p].l;i<=op[p].r;i++) b[i]+=op[p].m;
else
for (i=op[p].l;i<=op[p].r;i++)
a[i]=min(a[i],op[p].m-b[i]);
}
memset(b,0,sizeof(b));
for (p=1;p<=m;p++){
if (op[p].t==1)
for (i=op[p].l; i<=op[p].r; i++) b[i]+=op[p].m;
else{
k=-inf;
for(i=op[p].l;i<=op[p].r;i++)
k=max(k,a[i]+b[i]);
if(k!=op[p].m)
return false;
}
}
return true;
}
int main(){
int i;
scanf("%d%d",&n,&m);
for (i=1; i<=m; i++){
scanf("%d%d%d%d",&op[i].t,&op[i].l,&op[i].r,&op[i].m);
}
if (!solve()){
printf("NO\n");
}
else{
printf("YES\n");
for (i=1;i<=n;i++)
printf("%d ",a[i]);
cout<<endl;
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
题目链接:http://codeforces.com/contest/360/problem/A
题目的意思是说对于一个序列有两个操作:
1.(1,l,r,p)..将区间[l,r]所有数都加上p
2.(2,l,r,m).求出区间[l,r]的最大值为m
现在给你这个操作,问你时候能够找到原序列,任意输出一个正确答案即可。
这个题我没有想出来,看了大牛的题解。
这个题就是求出每个位置初始时可能的最小值,然后判断是否合法。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#define inf 1000000007
using namespace std;
struct node{
int t,l,r,m;
} op[5010];
int a[5010],b[5010],n,m;
bool solve(){
int p,i,k;
for (i=1;i<=n;i++){
a[i]=1000000000;
}
memset(b,0,sizeof(b));
for (p=1;p<=m;p++){
if (op[p].t==1)
for (i=op[p].l;i<=op[p].r;i++) b[i]+=op[p].m;
else
for (i=op[p].l;i<=op[p].r;i++)
a[i]=min(a[i],op[p].m-b[i]);
}
memset(b,0,sizeof(b));
for (p=1;p<=m;p++){
if (op[p].t==1)
for (i=op[p].l; i<=op[p].r; i++) b[i]+=op[p].m;
else{
k=-inf;
for(i=op[p].l;i<=op[p].r;i++)
k=max(k,a[i]+b[i]);
if(k!=op[p].m)
return false;
}
}
return true;
}
int main(){
int i;
scanf("%d%d",&n,&m);
for (i=1; i<=m; i++){
scanf("%d%d%d%d",&op[i].t,&op[i].l,&op[i].r,&op[i].m);
}
if (!solve()){
printf("NO\n");
}
else{
printf("YES\n");
for (i=1;i<=n;i++)
printf("%d ",a[i]);
cout<<endl;
}
return 0;
}
相关文章推荐
- CodeForces 360A - Levko and Array Recovery (思维)
- CodeForces - 361C A - Levko and Array Recovery 思维
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- CodeForces 360A - Levko and Array Recovery 给出操作求原始数列
- cf C. Levko and Array Recovery
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery
- CF-Codeforces Round #210 (Div. 1)-A-Levko and Array Recovery
- Codeforces Round #210 (Div. 1) B. Levko and Array(dp 思维)
- CodeForces 360A - Levko and Array Recovery (模拟)
- Codeforces 361C - Levko and Array Recovery(贪心)
- CodeForces 721D. Maxim and Array(贪心,思维题)
- cf D. Levko and Array
- Levko and Array
- 【Coder Force】#360B - Levko and Array(DP 二分枚举)
- Levko and Permutation CodeForces - 361B(思维)
- Codeforces Round #210 (Div. 1)——Levko and Array
- B. Levko and Array----二分答案
- codeforce 558B Amr and The Large Array 思维题
- codeforces D. Levko and Array(二分加dp) 挺好的一个题
- B. Levko and Array