• POJ 3061


    Subsequence
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7854   Accepted: 3021

    Description

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

    Input

    The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

    Output

    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3

    Source

     
    两种方法
    一种是用sum[i]数组存储从1 到 i 的和,然后二分长度。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 #define maxn 100005
     9 
    10 int n;
    11 int a[maxn],s,sum[maxn];
    12 
    13 bool check(int x) {
    14         for(int i = 1; i + x - 1 <= n; ++i) {
    15                 if(sum[i + x - 1] - sum[i - 1] >= s) return true;
    16         }
    17 
    18         return false;
    19 }
    20 
    21 
    22 
    23 void solve() {
    24         int l = 1,r = n + 1;
    25 
    26         while(l < r) {
    27                 int mid = (l + r) >> 1;
    28                 if(check(mid)) r = mid;
    29                 else l = mid + 1;
    30         }
    31 
    32         printf("%d
    ",l == n + 1 ? 0 : l);
    33 }
    34 
    35 int main()
    36 {
    37     //freopen("sw.in","r",stdin);
    38 
    39     int t;
    40     scanf("%d",&t);
    41 
    42     while(t-- ) {
    43             memset(sum,0,sizeof(sum));
    44             scanf("%d%d",&n,&s);
    45             for(int i = 1; i <= n; ++i) {
    46                     scanf("%d",&a[i]);
    47                     sum[i] += sum[i - 1] + a[i];
    48             }
    49 
    50             solve();
    51     }
    52 
    53     return 0;
    54 }
    View Code

    第二种是假设从s位置开始到t的和大于S,并且s + 1 到t' 的和大于S,则t‘ > t 由此可以跑一次o(n)解决

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 #define maxn 100005
     9 
    10 int n,S;
    11 int a[maxn];
    12 
    13 void solve() {
    14         int s = 1,sum = 0,pos = 1;
    15         int ans = n + 1;
    16 
    17         for(; ; ++s) {
    18             while(sum <= S && pos <= n) {
    19                 sum += a[pos++];
    20             }
    21             //printf("p = %d s = %d
    ",pos);
    22             if(sum < S) break;
    23             sum -= a[s];
    24             ans = min(ans,pos - s);
    25         }
    26 
    27         printf("%d
    ",ans == n + 1 ? 0 : ans);
    28 
    29 }
    30 
    31 int main() {
    32         //freopen("sw.in","r",stdin);
    33 
    34         int t;
    35         scanf("%d",&t);
    36         while(t--) {
    37                 scanf("%d%d",&n,&S);
    38                 for(int i = 1; i <= n; ++i) {
    39                         scanf("%d",&a[i]);
    40                 }
    41 
    42                 solve();
    43         }
    44 
    45         return 0;
    46 }
    View Code
     
  • 相关阅读:
    一道思考题所引起动态跟踪 ‘学案’
    Docker学习重点(3)~docker命令:帮助命令、镜像命令、容器命令、常用其他命令
    Docker学习重点(2)~docker安装、配置阿里云镜像加速
    Linux 常见必备
    Docker学习重点(4)~docker 部署环境
    本地虚拟机运行Linux的发行版软件(centos)部署项目
    Docker学习重点(1)~docker基本介绍
    AWS云服务器rpf配置踩坑记
    Asp.Net Core WebApi使用Swagger分组展示接口
    Asp.Net Core WebApi中集成Jwt认证
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3623263.html
Copyright © 2020-2023  润新知