zoukankan      html  css  js  c++  java
  • 每日编程-20170405

    题目描述
    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

    解答:

    暴力累加时间太长,只能用滑动窗口

     1 #include <iostream>
     2 #include <sstream>
     3 #include <algorithm>
     4 #include <numeric>
     5 #include <stack>
     6 #include <set>
     7 #include <map>
     8 #include <list>
     9 #include <deque>
    10 #include <string>
    11 #include <vector>
    12 #include <iterator>
    13 #include <math.h>
    14 using namespace std;
    15 int CrimTrans2(vector<int> v, int t, int c) {
    16     int answer = 0, sum = accumulate(v.begin(), v.begin() + c, 0);
    17     if (sum <= t)    answer++;
    18     for (auto i = 0; i + c != v.size() ; i++)
    19     {
    20         sum = sum - v[i] + v[i + c];
    21         if (sum <= t)    answer++;
    22         
    23     }
    24     return answer;
    25 }
    26 int main() {
    27     int n,t,c;
    28     while (cin >> n >> t>> c)
    29     {    
    30         int num;
    31         vector<int> v1;
    32         for (auto i = 0; i < n; i++)
    33         {
    34             cin >> num;
    35             v1.push_back(num);
    36         }
    37         cout << CrimTrans2(v1,t,c) << endl;
    38         
    39     }
    40 }
  • 相关阅读:
    求二维数组的最大子集和
    电梯调度《二》
    电梯调度问题
    BIEE 通过提示器选择展示列(列名接收表示变量)
    BIEE时间序列函数
    Python学习笔记(三)
    Python学习笔记(二)
    Python学习笔记(一)
    BIE前台常用设置
    BIEE 连接BW query query结构 引用
  • 原文地址:https://www.cnblogs.com/linhaowei0389/p/6670611.html
Copyright © 2011-2022 走看看