HDU 3486 rmq + binary

Source: Internet
Author: User
If you use rmq to accumulate the maximum number of segments, it will take a long time.

 
 
# Include <stdio. h> # include <string. h> const int max = 200010; # define min (a, B) a <B? A: B # define max (A, B) A> B? A: bint DP [Max] [30], a [Max]; int K; void Init (int n) {memset (DP, 0, sizeof (DP )); int I, j, M; for (I = 1; I <= N; I ++) {DP [I] [0] = A [I];} for (j = 1, m = 1; m <= N; m <= 1, J ++) for (I = N; I> = 1; I --) {if (I + (1 <(J-1) <= N) DP [I] [J] = max (DP [I] [J-1], DP [I + (1 <(J-1)] [J-1]);} int rmq (int l, int R) {int I, J, K,; k = r-L + 1; for (I = 0, j = 1; j <= K; j <= 1, I ++) {A = max (DP [l] [I], DP [R-J + 1] [I]);} return a ;}int getmax (INT per, int mid) {int I; int ans = 0; for (I = 1; I <= mid; I ++) {ans + = rmq (I-1) * per + 1, I * per); If (ANS> K) return ans;} return 0;} int main () {int N, I; while (scanf ("% d ", & N, & K), N> 0) {int sum = 0; int num = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]); if (a [I]> num) num = A [I]; sum + = A [I];} if (Num> K) {printf ("1 \ n"); continue;} If (sum <= k) {printf ("-1 \ n"); continue ;} init (n); // printf ("% d \ n", rmq (), rmq (); check rmqint L = 1, R = n, mid; int ans = 0; while (L <= r) {mid = (L + r)> 1; int per = N/Mid; int TMP = getmax (Per, mid); If (TMP> K) {r = mid-1; ans = mid;} else l = Mid + 1;} printf ("% d \ n ", ans);} return 0 ;}

 

Rmq format changed, 93 Ms !!!

# Include <string. h>
# Include <stdio. h>
# Include <math. h>
Const int max = 200005;
Int max (int A, int B) {return A> B? A: B ;}
Int DP [Max] [20], a [Max];
Int N, K, Val [Max];
Int log [Max];
Void make_rmq (int n, int B [])
{
Int I, J;
For (I = 1; I <= N; I ++)
DP [0] [I] = B [I];
For (I = 1; I <= log [N]; I ++)
{
Int Limit = n + 1-(1 <I );
For (j = 1; j <= limit; j ++)
DP [I] [J] = max (DP [I-1] [J], DP [I-1] [J + (1 <I> 1)]);
}
}
Int rmq (int l, int R)
{
Int K = log [R-l + 1];
Return max (DP [k] [L], DP [k] [R-(1 <k) + 1]);
}
Int getmax (INT per, int mid)
{
Int I;
Int ans = 0;
For (I = 1; I <= mid; I ++)
{
Ans + = rmq (I-1) * per + 1, I * per );
If (ANS> K) return ans;
}
Return 0;
}
Int get_val ()
{
Int ret (0 );
Char C;
While (C = getchar () = ''| C = '\ n' | C =' \ R ');
Ret = C-'0 ';
While (C = getchar ())! = ''& C! = '\ N' & C! = '\ R ')
Ret = RET * 10 + C-'0 ';
Return ret;
}
Int main ()
{
Int I;
Log [0] =-1;
For (I = 1; I <Max; I ++)
Log [I] = log [I> 1] + 1;
While (scanf ("% d", & N, & K), N> 0)
{
Int sum = 0;
Int num = 0;
For (I = 1; I <= N; I ++)
{
// Scanf ("% d", & Val [I]);
Val [I] = get_val ();
If (Val [I]> num) num = Val [I];
Sum + = Val [I];
}
If (Num> K) {printf ("1 \ n"); continue ;}
If (sum <= K)
{Printf ("-1 \ n"); continue ;}
Make_rmq (n, Val );
Int L = 1, R = N, mid;
Int ans = 0;
While (L <= r)
{
Mid = (L + r)> 1;
Int per = N/Mid;
Int TMP = getmax (Per, mid );
If (TMP> K)
{
R = mid-1;
Ans = mid;
}
Else l = Mid + 1;
}
Printf ("% d \ n", ANS );
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.