每日一题——Tallest Cow
题目
题解
我们可以假设一开始所有的牛都是最高的,这样子除了邻近的牛谁也看不见谁,然后若想让l,r两只牛相见,只需要把他们中间的牛高度-1即可,那就是将区间[l+1,r-1]的数字减1,所以我们可以想到差分的做法,但是请注意,题目说给的相见牛中可能会有重复数据,我们不能进行两次操作,所以我们要想办法去重,我这里用的是set去重,如果你想到这里,代码就应该没有难点了。可以试着敲代码了,注意差分的区间。
参考代码
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
const int N = 5010;
int n, p, h, A, B, m;
int a[N];
set<pair<int, int>> s;
int main(){
cin >> n >> p >> h >> m;
a[1] = h;
while(m --){
cin >> A >> B;
if(A > B) swap(A, B);
s.insert({A, B});
}
for(auto t : s){
a[t.first + 1] -= 1;
a[t.second] += 1;
}
for(int i = 1; i <= n; i ++){
a[i] += a[i - 1];
cout << a[i] << endl;
}
return 0;
}

浙公网安备 33010602011771号