Problem G:array C time limit:1 Sec Memory limit:128 MB
Description
Giving integers and and arrays and both with length, you should construct an array also with length which satisfied:
1.0≤Ci≤Ai(1≤i≤n)
2.
and make the value S is minimum. The value S is defined as:
Input
There is multiple test cases. In each test case, the first line contains integers n(1≤n≤1000) andm(1≤m≤100000). T Hen lines followed, each line contains n integers separated by spaces, indicating the array A and B in order. You can assume this 1≤Ai≤100 and 1≤Bi≤10000 for each i from 1 to n, and there must is at the least one solution for array C. The input would end by EOF.
Output
For each test case, the output of the minimum value S as the answer in one line .
Sample Input
3 4 2 3 41 1 1
Sample Output
6
Idea: First we will want to find the smallest to go down, but there is an an array for the upper bound is not good processing, so the C array is all modified to a array, has been reduced, when C is added equal to M to get the answer;
#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMoD 1000000007#defineINF 999999999#defineESP 0.00000000001//#pragma COMMENT (linker, "/stack:102400000,102400000")intScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}struct is{ll l,r; LL Maxx; ll num; ll sum;} tree[10010];ll a[1010];ll b[1010];voidBuildtree (ll l,ll r,ll pos) {TREE[POS].L=l; TREE[POS].R=S; if(l==r) {Tree[pos].num=A[l]; Tree[pos].maxx= (a[l]*a[l]-(a[l]-1) * (a[l]-1))*B[l]; Tree[pos].sum=A[l]; return; } ll Mid= (l+r)/2; Buildtree (L,mid,pos*2); Buildtree (Mid+1, r,pos*2+1); Tree[pos].maxx=max (tree[pos*2].maxx,tree[pos*2+1].maxx); Tree[pos].sum=tree[pos*2+1].sum+tree[pos*2].sum;}voidUpdate (ll l,ll r,ll pos,ll change) {if(tree[pos].r==change&&tree[pos].l==Change ) {ll KK=tree[pos].num-1; Tree[pos].num=KK; Tree[pos].maxx= (kk*kk-(kk-1) * (kk-1))*B[change]; Tree[pos].sum=KK; return; } ll Mid= (l+r)/2; if(change<=mid) Update (L,mid,pos*2, change); ElseUpdate (Mid+1, r,pos*2+1, change); Tree[pos].sum=tree[pos*2].sum+tree[pos*2+1].sum; Tree[pos].maxx=max (tree[pos*2].maxx,tree[pos*2+1].maxx);} ll Findmax (ll l,ll r,ll pos,ll gg) {if(tree[pos].l==TREE[POS].R)returnTREE[POS].L; LL Mid= (l+r)/2; if(tree[pos*2].maxx==gg)returnFindmax (l,mid,pos*2, GG); Else returnFindmax (mid+1, r,pos*2+1, GG);} ll Getans (ll l,ll r,ll Pos) {if(l==R)returntree[pos].num*tree[pos].num*B[l]; LL Mid= (l+r)/2; returnGetans (l,mid,pos*2) +getans (mid+1, r,pos*2+1);}intMain () {ll x,y,z,i,t; while(~SCANF ("%lld%lld",&x,&y)) { for(i=1; i<=x;i++) scanf ("%lld",&A[i]); for(i=1; i<=x;i++) scanf ("%lld",&B[i]); Buildtree (1X1); while(tree[1].sum!=y) {ll pos=findmax (1X1, tree[1].maxx); Update (1X1, POS); } printf ("%lld\n", Getans (1X1)); } return 0;}
Huazhong Agricultural University fourth session program design Competition network synchronization G.array C segment tree or priority queue