题目描述
思路分析
差分。
所有
[
a
,
b
]
[a,b]
[a,b]区间必然不交叉,否则有矛盾。初始高度全设为
h
h
h,读入每个区间
[
a
,
b
]
[a,b]
[a,b],让区间每个数都
−
1
-1
−1(因为要让每头牛最高),最后差分序列求原序列。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int heights[N];
int n,p,h,m;
set<pair<int,int>> has;
int main(){
cin>>n>>p>>h>>m;
heights[1]=h;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
if(a>b) swap(a,b);
if(!has.count({a,b})){
has.insert({a,b});
heights[a+1]--,heights[b]++;
}
}
for(int i=1;i<=n;i++){
heights[i]+=heights[i-1];
cout<<heights[i]<<endl;
}
return 0;
}
1201

被折叠的 条评论
为什么被折叠?



