秦腾与教学评估【前缀和+二分】

yxc,说着是一道经典的题。我就写题解。

首先,题目的意思是给出几个等差序列,然后对应的等差数列上的坐标上放一个防具。而只有奇数个防具的点的才能被攻破。所以目的就是找出奇数个那个防点的位置。


首先我们知道,an = a1 + (n-1)d

所以可以知道其中有多少数(an – a1)/d + 1;

所以二分答案,取每个点的坐标,不说看代码


  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 using namespace std;
  5 
  6 const int N = 2e5 + 5;
  7 
  8 struct node{
  9     int s, e, d;
 10 };
 11 
 12 node DC[N];
 13 int n;
 14 
 15 typedef long long LL;
 16 
 17 LL get_num(int x)
 18 {
 19     LL res = 0;
 20     for (int i = 0; i < n; ++ i)
 21         if(DC[i].s <= x)
 22             res += (min(DC[i].e, x) - DC[i].s)/DC[i].d + 1;
 23     return res;
 24 }
 25 
 26 int main(){
 27     ios::sync_with_stdio(false);
 28     cin.tie(0),cout.tie(0);
 29     int T;
 30     cin >> T;
 31     while(T--){
 32         int  l = 0, r = 0;
 33         cin >> n;
 34 
 35         for(int i = 0; i < n; ++ i)
 36         {
 37             int s, e, d;
 38             cin >> s >> e >> d;
 39             DC[i] = {s, e, d};
 40             r = max(r, e);
 41         }
 42 
 43         while(l < r){
 44             int mid = l + r >> 1;
 45             if (get_num(mid) & 1) r = mid;
 46             else l = mid + 1;
 47         }
 48         auto sum = get_num(r) - get_num(r-1);
 49         if(sum & 1) cout << r << " " << sum << endl;
 50         else puts("There's no weakness.");
 51     }
 52 }
 53 
 54 




原文地址:https://www.cnblogs.com/rstz/p/12747582.html