标签:algorithm 有一个 rgba ons diff += 前缀 iostream 决策
*前置知识:
1 for(int i=1;i<=n;i++) 2 {cin>>a[i];sum[i]=sum[i-1]+a[i];} 3 for(int i=1;i<=q;i++) 4 {cin>>l>>r;cout<<sum[r]-sum[l-1]<<" ";}
1 for(int i=1;i<=n;i++) 2 {cin>>diff[i];a[i]=diff[i]+a[i-1];} 3 for(int i=1;i<=n;i++) 4 {cout<<a[i];}
*代码实现:
1 for (int i = 1;i <= n;i++){ 2 need[i]=need[i-1]+dis[i]; 3 if (need[i]>room[i]) return false; 4 }
1 while (ll<rr){ 2 int mid=(ll+rr)>>1; 3 if (solve(mid)) ll=mid+1; 4 else rr=mid; 5 }
完整代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 const int maxn=1e6+10; 7 int n,m; 8 int room[maxn],l[maxn],r[maxn],d[maxn]; 9 int dis[maxn],need[maxn]; 10 bool solve(int x){ 11 memset(dis,0,sizeof(dis)); 12 for (int i = 1;i <= x;i++){ 13 dis[l[i]]+=d[i]; dis[r[i]+1]-=d[i]; 14 } 15 for (int i = 1;i <= n;i++){ 16 need[i]=need[i-1]+dis[i]; 17 if (need[i]>room[i]) return false; 18 } 19 return true; 20 } 21 int main(){ 22 scanf ("%d%d",&n,&m); 23 for (int i = 1;i <= n;i++) scanf ("%d",&room[i]); 24 for (int i = 1;i <= m;i++) scanf ("%d%d%d",&d[i],&l[i],&r[i]); 25 if (solve(m)) {printf("0\n");return 0;} 26 int ll = 1,rr=m; 27 while (ll<rr){ 28 int mid=(ll+rr)>>1; 29 if (solve(mid)) ll=mid+1; 30 else rr=mid; 31 } 32 printf("-1\n%d\n",ll); 33 return 0; 34 }
标签:algorithm 有一个 rgba ons diff += 前缀 iostream 决策
原文地址:https://www.cnblogs.com/very-beginning/p/13791602.html