zoukankan      html  css  js  c++  java
  • UVALive 2678 大于s的最短子序列和

    input

    n s 10<=n<=100000,s<1e9

    a1 a2 ... an     ai<=10000

    output

    大于s的最短子序列和的长度,没有输出0

     1 #include <cstdio>
     2 #include <queue>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <cstdlib>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <ctime>
    11 #include <cmath>
    12 #define MAX 100000
    13 
    14 using namespace std;
    15 int n,s,a[MAX+10];
    16 int main()
    17 {
    18     freopen("/home/user/桌面/in","r",stdin);
    19     while(scanf("%d%d",&n,&s)==2)
    20     {
    21         int ans=0x7fffffff;
    22         scanf("%d",&a[0]);
    23         for(int i=1;i<n;i++)
    24         {
    25             scanf("%d",&a[i]);
    26             a[i]+=a[i-1];
    27         }
    28         int p=0;
    29         for(int i=0;i<n;i++)
    30         {
    31             if(a[i]-s>0) p=lower_bound(&a[p],a+i,a[i]-s)-a;
    32             if(p>0) ans=min(ans,i-p+1);
    33         }
    34 /*        for(int i=0,j=0;i<n;i++)
    35         {
    36             if(a[j]>a[i]-s) continue;
    37             while(a[j]<a[i]-s) j++;
    38             ans=min(ans,i-j+1);
    39         }*/
    40         a[n-1]>=s?printf("%d
    ",ans):puts("0");
    41     }
    42     //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    【排序】题解_P1093奖学金
    简单了解连接服务器的要求
    centos7安装(纯文字版)
    JAVA虚拟机
    集合
    IO流
    反射
    多线程
    JAVA基础
    博客园皮肤文档
  • 原文地址:https://www.cnblogs.com/cdyboke/p/4995876.html
Copyright © 2011-2022 走看看