题解 | #To Fill or Not to Fill#

To Fill or Not to Fill

https://www.nowcoder.com/practice/f7eba38f7cd24c45982831e0f38518f9

#include <iostream>
#include <map>
using namespace std;

int main() {
    int cmax, d, davg, n;
    while (cin >> cmax >> d >> davg >> n) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        multimap<double, int> stationMap;
        while (n--) {
            double price;
            int distance;
            cin >> price >> distance;
            stationMap.insert(make_pair(price, distance));
        }
        int farest = cmax * davg;
        bool way[d + 1];
        for (int i = 1; i < d + 1; i++) {
            way[i] = false;
        }
        double price = 0;
        for (auto it = stationMap.begin(); it != stationMap.end(); it++) {
            double tempPrice = it->first;
            int index = it->second;
            int singleDistance = 0;
            for (int i = index + 1; i <= d && i <= index + farest; i++) {
                if (way[i] == false) {
                    way[i] = true;
                    singleDistance++;
                }
            }
            price += singleDistance / (davg * 1.0) * tempPrice;
        }
        int distance = 0;
        bool finish = true;
        for (int i = 1; i <= d; i++) {
            if (way[i]) distance++;
            else {
                finish = false;
                break;
            }
        }
        if(finish){printf("%.2f\n",price);}
        else printf("The maximum travel distance = %.2f\n",(double)distance);
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-19 13:59
门头沟学院 Java
用微笑面对困难:Trae一下,如果真成了,他用了直接发字节起诉代码版权,,这个代码不商用是没问题的如果没成也是情理之中的。
点赞 评论 收藏
分享
10-17 09:06
门头沟学院 Java
LZStarV:(这居然能又刷到你)感觉真的就是岗位少了吧,项目很多27届这个点都差不多这个水准,只能说有点不赶趟了先慢慢投吧兄弟,啥都投一下总有机会的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务