UVALive 6609 Minimal Subarray Length (查找+构建排序数组)

描述:给定n个整数元素,求出长度最小的一段连续元素,使得这段元素的和sum >= X.

对整个数组先求出sum[i],表示前i个元素的和,然后依次求出以a[i]为起点的,总和>= X的最小长度,

每次考虑新元素a[i]时,将a[i]加入数组, pa[—q] = mp(sum[i], i),这样pa[q……….p]形成一段总和递增的序列,下标也是逐渐增大。

最后利用lower_bound函数求出大于或等于sum[i-1]+X的最左边的元素,求出以i为起点最短长度。

最后想了一下如果是求>= X的最大长度的序列,应该这样做,每次和pa数组中最大元素比较,如果比最大元素还大则加入到数组末尾,这样同样利用lower_bound函数可以求出最大长度序列的末尾元素的下标。

//Date: 20140211
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define  esp 1e-6
#define  pi acos(-1.0)
#define  inf 0x0f0f0f0f
#define  pb push_back
#define  lson l, m, rt<<1
#define  rson m+1, r, rt<<1|1
#define  mp(a, b) make_pair((a), (b))
#define  in  freopen("solve_in.txt", "r", stdin);
#define  out freopen("solve_out.txt", "w", stdout);
#define  bug puts("********))))))");
#define  inout in out
#define  stop  system("pause");
#define  PRD(a) printf("%d\n",(a))
#define  PRLD(a) printf("%lld\n", (a))
#define  PRID(a) printf("%I64d\n", (a))
#define  PRU(a) printf("%u\n", (a))
#define  PRLU(a) printf("%llu\n", (a))
#define  PRIU(a) printf("%I64u\n", (a))
#define  SET(a, v) memset(a, (v), sizeof(a))
#define  READ(a, n) {REP(i, n) cin>>a[i];}
#define  REP(i, n) for(int i = 0; i < (n); i++)
#define  Rep(i, base, n) for(int i = base; i < n; i++)
#define  REPS(s) for(int i = 0; s[i]; i++)
#define  pf(x) ((x)*(x))
#define  Log(a, b) (log((double)b)/log((double)a))
#define Srand() srand((int)time(0))
#define random(number) (rand()%number)
#define random_range(a, b) (int)(((double)rand()/RAND_MAX)*(b-a) + a)

using namespace std;
typedef long long  LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
typedef vector<pii> VII;
typedef vector<pii, int> VIII;
typedef VI:: iterator IT;
typedef map<string, int> Mps;
typedef map<int, int> Mpi;
typedef map<int, pii> Mpii;
typedef map<pii, int> Mpiii;
const int maxn = 500000 + 1090;
LL sum[maxn];
int a[maxn];
pair<LL, int> pa[maxn];
int main()
{
    int T;
    for(int t = scanf("%d", &T); t<= T; t++) {
        int n;
        LL X;
        scanf("%d%lld", &n, &X);
        Rep(i, 1, n+1) scanf("%d", a+i), sum[i] = sum[i-1] + a[i];
        int ans = n+1;
        int p , q;
        p = q = n+1;
        for(int i = n; i >= 1; i--) {
            while(q < p && pa[q].first <= sum[i]) q++;//寻找比sum[i]大的和,将sum[i]放在其后面
            pa[--q] = mp(sum[i], i);//形成和从大到小,下标由大到小的顺序;
            int low = lower_bound(pa+q, pa+p, mp(sum[i-1] + X, 0)) - pa;//查找比sum[i-1]+X大的最左边的元素。
            if(low < p) ans = min(ans, pa[low].second - i+1);
        }
        printf("%d\n", (ans == n+1) ? -1 : ans);
    }
    return 0;
}

你可能感兴趣的:(length)