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

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 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值