最高的牛

image

题目分析:

image

本题的运用差分思想,所以呢

证明差分序列的前缀和是原序列


前缀和的逆运算::

a[1],a[2],...a[n];

b[i] = a[i] - a[i-1];b[1] = a[1];

a[i] = b[1]+b[2]+...+b[i];

a[i] = b[1] + b[2] + b[3] +...+ b[i];//证明a[i]是b[i]的前缀和序列

= a[1] + a[2] - a[1] + a[3] - a[2] +......+a[i] - a[i-1];

= a[i];

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <set>
  5 using namespace std;
  6 set<pair<int,int>> existed;
  7 int height[10010];//除f[i]以外存的,是差值
  8 int main(){
  9     int n, p, h, m;
 10     cin >> n >> p >> h >> m;
 11     height[1] = h;
 12     for(int i = 0, a, b; i < n; ++ i){
 13         cin >> a >> b;
 14         if(a > b) swap(a, b);
 15         if(existed.count(make_pair(a,b))) continue;//去重
 16         existed.insert(make_pair(a,b));
 17         height[a + 1]--, height[b]++;//差分
 18     }
 19     for(int i = 1; i <= n; ++ i)
 20     {
 21         cout << height[i] << endl;
 22         height[i + 1] += height[i];
 23     }
 24     return 0;
 25 }
追求吾之所爱
原文地址:https://www.cnblogs.com/rstz/p/12685809.html