zoukankan      html  css  js  c++  java
  • 2016百度编程题:罪犯转移

    题目描述

    C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式? 
    输入描述:
    第一行数据三个整数:n,t,c(1≤n≤2e5,0≤t≤1e9,1≤c≤n),第二行按入狱时间给出每个犯人的罪行值ai(0≤ai≤1e9)
    输出描述:
    一行输出答案。
    输入例子:
    3 100 2
    1 2 3
    输出例子:
    2
    解题

    暴力超时,然后想到滑动窗口形式,每次减去第一个数,加上临近的下一个数。中间下标自己搞乱了,就用了break
    import java.util.Scanner;
    public class Main{
        public static void main(String [] args){
            Scanner in = new Scanner(System.in);
            while(in.hasNext()){
                int n = in.nextInt();
                int t = in.nextInt();
                int c = in.nextInt();
                int[] A = new int[n];
                for(int i = 0;i<n;i++)
                    A[i] = in.nextInt();
                int count = 0;
                int sum = 0;
                for(int i = 0;i<c;i++){
                    sum+=A[i];
                }
                for(int i = 0;i< n;i++){
                    if(sum<=t){
                        count++;
                    }
                    if(i  +c >= n)
                        break;
                    sum = sum - A[i] + A[i+c];
                }
                System.out.println(count);
            }
        }
    }
  • 相关阅读:
    容器适配器之queue
    STL之deque
    STL之list
    STL之multiset
    STL之multimap
    STL之set
    string
    命名空间
    Windows Live Writer教程及代码高亮工具
    STL之vector
  • 原文地址:https://www.cnblogs.com/theskulls/p/5306737.html
Copyright © 2011-2022 走看看