zoukankan      html  css  js  c++  java
  • Books

    Books

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5824

    DreamGrid went to the bookshop yesterday. There are  books in the bookshop in total. Because DreamGrid is very rich, he bought the books according to the strategy below:

    • Check the  books from the 1st one to the -th one in order.
    • For each book being checked now, if DreamGrid has enough money (not less than the book price), he'll buy the book and his money will be reduced by the price of the book.
    • In case that his money is less than the price of the book being checked now, he will skip that book.

    BaoBao is curious about how rich DreamGrid is. You are asked to tell him the maximum possible amount of money DreamGrid took before buying the books, which is a non-negative integer. All he knows are the prices of the  books and the number of books DreamGrid bought in total, indicated by .

    Input

    There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

    The first line contains two integers  and  (), indicating the number of books in the bookshop and the number of books DreamGrid bought in total.

    The second line contains  non-negative integers  (), where  indicates the price of the -th book checked by DreamGrid.

    It's guaranteed that the sum of  in all test cases will not exceed .

    Output

    For each test case output one line.

    If it's impossible to buy  books for any initial number of money, output "Impossible" (without quotes).

    If DreamGrid may take an infinite amount of money, output "Richman" (without quotes).

    In other cases, output a non-negative integer, indicating the maximum number of money he may take.

    Sample Input

    4
    4 2
    1 2 4 8
    4 0
    100 99 98 97
    2 2
    10000 10000
    5 3
    0 0 0 0 1
    

    Sample Output

    6
    96
    Richman
    Impossible
    

     模拟题,先把0的个数判断出来,后面贪心即可

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<string>
     6 #include<cmath>
     7 #include<vector>
     8 #include<queue>
     9 #define maxn 200005
    10 #define INF 0x3f3f3f3f3f3f3f3f
    11 using namespace std;
    12 
    13 long long a[maxn];
    14 
    15 int main(){
    16     int t;
    17     cin>>t;
    18     int n,m;
    19     while(t--){
    20         long long ans=0;
    21         cin>>n>>m;
    22         long long Min=INF;
    23         int zero=0;
    24         for(int i=1;i<=n;i++){
    25             cin>>a[i];
    26             if(!a[i]) zero++;
    27             if(Min>a[i]) Min=a[i];
    28         }
    29         if(n==m) cout<<"Richman"<<endl;
    30         else{
    31             if(m==0){
    32                 if(Min==0) cout<<"Impossible"<<endl;
    33                 else cout<<Min-1<<endl;
    34             }
    35             else{
    36                 int i,flag=0;
    37                 m-=zero;
    38                 if(m<0) flag=1;
    39                 int co=0;
    40                 for(i=1;i<=n&&co<m;i++){
    41                     if(a[i]){
    42                         ans+=a[i];
    43                         co++;
    44                     }
    45                 }
    46                 int tmp=0x3f3f3f3f;
    47                 for(;i<=n;i++){
    48                     if(tmp>a[i]&&a[i]){
    49                         tmp=a[i];
    50                     }
    51                 }
    52                 ans+=tmp-1;
    53                 if(flag){
    54                     cout<<"Impossible"<<endl;
    55                 }
    56                 else{
    57                     cout<<ans<<endl;
    58                 }
    59             }
    60         }
    61     }
    62 
    63 }
    View Code
  • 相关阅读:
    隐马尔科夫模型(Hidden Markov Models) 系列之三
    隐马尔科夫模型(Hidden Markov Models) 系列之二
    隐马尔科夫模型(Hidden Markov Models) 系列之一
    详解大端模式和小端模式
    viewController详解
    手机产品设计禁忌
    直接拿来用!最火的iOS开源项目(三)
    直接拿来用!最火的iOS开源项目(二)
    <c:out>标签不能正确输出value中的值
    元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9908769.html
Copyright © 2011-2022 走看看