【2016 泉市教科】plusplus - Pascal

plusplus

plusplus

题解

  这类题目,有一种替代的高效方法,就是对每一个操作,在b[l]和b[r]打上标记,然后对整个数组扫一遍判断。但这样的做法会提高时间复杂度,因而我们要去减少处理次数。
  这里我们先读入每个数字a[i],之后预处理一遍a[i]:=a[i]-a[i-1],计算出每个点的“参差不齐”的程度。而每一次题目所给的区间处理,是对该段区间同增同减,区间内相邻点的差值保持不变,只有该段区间与其头尾两个相邻的点差值发生改变,这样只要对头尾的两个数处理就可以了。并且用一个cnt记录当前有多少个点不等于0,直到cnt = 0结束程序。
  附上Pascal代码:
  

program pluspluse;
var a:array[0..1000001] of int64;
    k:int64;
    n,m,i,j,x,y,cnt:longint;

begin
  read(n,m);
  for i:=1 to n do
     read(a[i]);
  for i:=n downto 1 do
     a[i]:=a[i]-a[i-1];
  for i:=1 to n do
     if a[i] <> 0 then inc(cnt);
  for i:=1 to m do
    begin
      read(x,y,k);
      y:=y+1;
      if (a[x] <> 0) and (a[x]-k = 0) then dec(cnt);
      if (a[x] = 0) and (a[x]-k <> 0) then inc(cnt);
      if (y<=n) and (a[y]=0) and (a[y]+k<>0) then inc(cnt);
      if (y<=n) and (a[y]<>0) and (a[y]+k = 0) then dec(cnt);
      if cnt=0 then begin writeln(i);halt;end;
      a[x]:=a[x]-k;
      a[y]:=a[y]+k;
    end;
  writeln('-1');
  end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值