2015 Multi-school Joint training first field assignment (hdu5289) three kinds of solution

Source: Internet
Author: User

Topic: Give a series, ask how many consecutive sub-sequences, the maximum value of the subsequence-the minimum value

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>using namespace STD;intmaxsum[100000][ -];intminsum[100000][ -];inta[100000];intN,k;voidRmq_init () { for(intj =1; (1&LT;&LT;J) <= N; ++J) for(inti =1; i + (1&LT;&LT;J)-1<= N; ++i) {Maxsum[i][j] = max (maxsum[i][j-1],maxsum[i+ (1<< (J-1))][j-1]); Minsum[i][j] = min (minsum[i][j-1],minsum[i+ (1<< (J-1))][j-1]); }}intQueryintLintR) {intK = log2 (r-l+1);intmax = max (Maxsum[l][k], maxsum[r-(1&LT;&LT;K) +1][k]);intmin = min (minsum[l][k], minsum[r-(1&LT;&LT;K) +1][k]);returnMax-min;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%d%d", &n,&k); for(inti =1; I <= n;++i) {scanf("%d", a+i); maxsum[i][0] = minsum[i][0] = A[i]; } rmq_init ();Long LongAns =0;intL, R; for(inti =1; I <= N; ++i) {L = i, r = N; while(L <= R) {intMid = (l+r)/2;intCha = query (i,mid);if(Cha < k) L = mid+1;ElseR = Mid-1;        } ans + = l-i; }printf("%lld\n", ans); }return 0;}

Two: Monotone queue
Maintain a minimum value with a monotone queue, double pointers, the first second pointer initially points to the first data, the first pointer continuously adds data to the end of the team in order, and when the difference of the maximum minimum value is greater than or equal to K, it means that the newly added one cannot be applied to the position of the current second pointer, and can be calculated The number of successive sub-sequences starting at the second pointer position, and the last statistic sum.

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace STD;#define LL Long Long deque <LL>Max, Min;//monotone queue, Max Max, min minLL a[100010] ;intMain () {intT, N, I, J; LL k, ans;scanf("%d", &t); while(t--) {scanf("%d%i64d", &n, &k); for(i =0; I < n; i++)scanf("%i64d", &a[i]); while( ! Max.empty ()) Max.pop_back (); while( ! Min.empty ()) Min.pop_back (); for(i =0, j =0, ans =0; I < n; i++)//i in front, J in the back{ while( !            Max.empty () && max.back () < a[i]) Max.pop_back (); Max.push_back (A[i]); while( !            Min.empty () && min.back () > A[i]) min.pop_back (); Min.push_back (A[i]); while( ! Max.empty () &&! Min.empty () && Max.front ()-Min.front () >= k) {ans + = (i-j);if(Max.front () = = A[j]) Max.pop_front ();if(Min.front () = = A[j])                Min.pop_front ();            j + +; }        } while(J < N)            {ans + = (i-j);        j + +; }printf("%lld\n", ans); }return 0;}

Three: rmq+ greedy
This enumeration right endpoint, greedy to select the right endpoint (similar to the ruler)

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>using namespace STD;intmaxsum[100000][ -];intminsum[100000][ -];inta[100000];intN,m;voidRmq_init () { for(intj =1; (1&LT;&LT;J) <= N; ++J) for(inti =1; i + (1&LT;&LT;J)-1<= N; ++i) {Maxsum[i][j] = max (maxsum[i][j-1],maxsum[i+ (1<< (J-1))][j-1]); Minsum[i][j] = min (minsum[i][j-1],minsum[i+ (1<< (J-1))][j-1]); }}intQueryintLintR) {intK = log2 (r-l+1);intmax = max (Maxsum[l][k], maxsum[r-(1&LT;&LT;K) +1][k]);intmin = min (minsum[l][k], minsum[r-(1&LT;&LT;K) +1][k]);returnMax-min;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); for(inti =1; I <= N; ++i) {scanf("%d", a+i); maxsum[i][0] = minsum[i][0] = A[i]; } rmq_init ();Long LongAns =0;intk=1; for(intI=1; i<=n; i++) { while(Query (k,i) >=m&&k<i) k++; ans+= (i-k+1); }printf("%lld\n", ans); }return 0;}

The following are three algorithms run time (3,2,1)
Visible time Efficiency monotone queue best (405ms), greedy +rmq (733ms) Second, +RMQ (1216ms) worst

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2015 Multi-school Joint training first field assignment (hdu5289) three kinds of solution

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.