开始一直re 检查不到数组越界。。后来才发现原来是没看到这句 The liquid that overflows from the n-th vessel spills on the floor.
因此要限定p<n
此外 还会超时;
///
本以为就这么简单,没想到数据太变态。。一直超时。。。
......最后的最后发现,数据没问题,算法也没问题,居然是实现的过程漏了一个“巨大”的小细节。代码中带*行。
代码:
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; int n; int vol[200050]; int water[200050]; int pour_to[200050]; int main() { int i,p,x,m; scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&vol[i]); pour_to[i]=i+1; } scanf("%d",&m); int c; for (i=1;i<=m;i++) { scanf("%d",&c); if (c==1) { scanf("%d%d",&p,&x); if (p>n||p<=0) continue; while (x-(vol[p]-water[p])>0) { if (p>n||p<=0) break; x-=vol[p]-water[p]; water[p]=vol[p]; // int ok=0; int tmp=p; while (water[pour_to[p]]==vol[pour_to[p]]&&pour_to[p]<=n) { p=pour_to[p]; ok=1; } if (ok) pour_to[tmp]=p; //*****************************一直忘记写这句,没这句就相当于没有跳跃,和不存在pour_to数组一样,难怪超时 ///跳跃---防超时 if (!ok) p++; } if (p>n||p<=0) continue; if (x-(vol[p]-water[p])<=0) water[p]+=x; } if (c==2) { int y; scanf("%d",&y); printf("%d\n",water[y]); continue; } } return 0; }