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

全部评论

相关推荐

26牛牛不会梦到感谢信:羡慕离职了还能吃吗现在就赶回去
点赞 评论 收藏
分享
菜鸡29号:根据已有信息能初步得出以下几点: 1、硕士排了大本和大专 2、要求会多语言要么是招人很挑剔要么就是干的活杂 3、给出校招薪资范围过于巨大,说明里面的薪资制度(包括涨薪)可能有大坑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务