codeforces gym 101933 House Lawn (字符串处理)详解

H. House Lawn

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

You have just bought a new house, and it has a huge, beautiful lawn. A lawn that needs cutting. Several times. Every week. The whole summer.

After pushing the lawnmower around the lawn during the hottest Saturday afternoon in history, you decided that there must be a better way. And then you saw the ads for the new robotic lawnmovers. But which one should you buy? They all have different cutting speeds, cutting times and recharge times, not to mention different prices!

According to the advertisement, a robotic lawnmover will spend all its time either cutting the lawn or recharging its battery. Starting from a full battery, it will cut the lawn at a given rate of cc square meters per minute for a cutting time of tt minutes, after which it has run out of battery. Once out of battery, it will immediately start recharging. After recharging for rr minutes the battery is full again and it immediately starts cutting.

You decide that in order for your lawn to look sufficiently prim and proper, the lawnmower that you buy must be powerful enough to cut your whole lawn at least once a week on average. Formally, if we start the mower fully charged at the beginning of the week and run it for exactly TT weeks, it needs to cut the whole lawn at least TT times, for all positive integers TT . But apart from this, you have no specific requirements, so among the ones that satisfy this requirement, you will simply go for the cheapest option. For the purposes of cutting your lawn, you may make the simplifying assumption that a week is always exactly 1008010080 minutes long.

Input

The first line of input contains two integers ℓℓ and mm (1≤ℓ≤1061≤ℓ≤106 , 1≤m≤1001≤m≤100 ), the size of your lawn in square meters, and the number of lawnmowers to consider, respectively.

Then follow mm lines, each containing a string nn and 44 integers pp , cc , tt , and rr , separated by commas, describing a lawnmower as follows:

  • nn is the name of the lawnmower, a string of at most 6060 printable characters (ASCII 3232 to 126126 ) excluding ',', neither starting nor ending with a space,
  • 1≤p≤1000001≤p≤100000 is the price of the lawnmover,
  • 1≤c≤1001≤c≤100 is the cutting rate in square meters per minute,
  • 1≤t≤100801≤t≤10080 is the cutting time in minutes, and
  • 1≤r≤100801≤r≤10080 is the recharge time in minutes.

Output

Output the name of the cheapest lawnmower capable of cutting your whole yard at least once a week on average. If several lawnmovers share the same lowest price, output all of their names, in the same order they were given in the input. If there is no such mower, output "no such mower".

题目的很好读,这里就不加以解释,就是寻找满足条件的物品中价格最小的(若有多个价格最小的能满足每周一次的工作频率的机器人,则按输入顺序输出机器人的名字)。

这题的最难的地方不是求出答案,而是处理输入的数据。下面附上代码

    int mip = 0x3f3f3f3f;
    scanf("%d%d", &n, &m);
   
    for(int i = 0; i < m; i++){
        char ch;
        int k = 0;
        scanf(" %c", &ch);

        while(ch != ','){
            arr[i].s[k++] = ch;
            scanf("%c", &ch);
        }

        scanf("%d%c%d%c%d%c%d%c", &arr[i].p, &ch, &arr[i].c, &ch, &arr[i].t, &ch, &arr[i].r, &ch);//用%c吃掉逗号,最后一个%c吃掉回车
        if(isok(i)){
            mip = min(mip, arr[i].p);
        }
    }

因为每行数据之间的各种数据是以逗号隔开的,所以我们用%c直接吃掉逗号,然后就相当于输入整数了,这样可以避免将字符串转为整数的操作,特别需要注意的是每个机器人的名字中是含有空格的,所以不能直接用%s输入,(由于博主cin用的少,不确定cin能不能实现我这样的操作),处理完输入数据后就是判断该机器人能否达到么周一次的工作频率了。

下面附上全代码

#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stdio.h>
#define LL long long
using namespace std;

const int MAX = 105;
struct date
{
    char s[100];
    int p;
    int c;
    int t;
    int r;
    double tt;
}arr[MAX];
int n, m;

bool isok(int x){
    int all = arr[x].c * arr[x].t; //充满一次电工作的总时长
     double ti = n * 1.0 / all * (arr[x].t + arr[x].r);
     arr[x].tt = ti;
     return ti <= 10080;
}

int main(){
    int mip = 0x3f3f3f3f;
    scanf("%d%d", &n, &m);
   
    for(int i = 0; i < m; i++){
        char ch;
        int k = 0;
        scanf(" %c", &ch);

        while(ch != ','){
            arr[i].s[k++] = ch;
            scanf("%c", &ch);
        }

        scanf("%d%c%d%c%d%c%d%c", &arr[i].p, &ch, &arr[i].c, &ch, &arr[i].t, &ch, &arr[i].r, &ch);
        if(isok(i)){
            mip = min(mip, arr[i].p);
        }
    }

  
    if(mip == 0x3f3f3f3f){
        printf("no such mower\n");
    } else{
        for(int i = 0; i < m; i++){
            if(arr[i].p == mip && arr[i].tt <= 10080){
                printf("%s\n", arr[i].s);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/88387988
今日推荐