题解 | #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")

全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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