zoukankan      html  css  js  c++  java
  • 1085. Perfect Sequence

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a “perfect sequence” if M <= m * p where M and m are the maximum and minimum numbers in the sequence, respectively.
    Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.

    Input Specification:
    Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (<= 105) is the number of integers in the sequence, and p (<= 109) is the parameter. In the second line there are N positive integers, each is no greater than 109.

    Output Specification:
    For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.

    Sample Input:
    10 8
    2 3 20 4 5 1 6 7 8 9
    Sample Output:
    8

    知识点:

    剪枝,遍历的时候,如果 list[i]*p < list[j] 的话,就剪枝

    另外,比当前完美序列长度小的就不用遍历了,j 直接从 i+cnt 开始

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main(){
     7     vector<int> list;
     8     long long p,tmp;
     9     int n; scanf("%d %lld",&n,&p);
    10     
    11     for(int i=0;i<n;i++){
    12         scanf("%lld",&tmp);
    13         list.push_back(tmp);
    14     }
    15     sort(list.begin(),list.end());
    16     
    17     int cnt=0;
    18     
    19     for(int i=0;i<n;i++){
    20         for(int j=i+cnt;j<n;j++){
    21             if(list[i]*p>=list[j]){
    22                 if(j-i>cnt)
    23                     cnt=j-i;
    24             }else{
    25                 break;
    26             }
    27         }
    28     }
    29     printf("%d",cnt+1);
    30 }
  • 相关阅读:
    Mac安装Homebrew的那些事儿
    SpringBoot:如何优雅地处理全局异常?
    JDK8日常开发系列:Consumer详解
    Docker 快速安装Jenkins完美教程 (亲测采坑后详细步骤)
    Linux安装Git-两种方式详细教程)
    Linux安装maven(详细教程)
    Linux安装jdk(详细教程)
    Docker基础概念与安装
    JVM参数及调优
    JDK内置工具命令
  • 原文地址:https://www.cnblogs.com/lokwongho/p/9950435.html
Copyright © 2011-2022 走看看