1. 程式人生 > >CodeForces 215D Hot Days(貪心)

CodeForces 215D Hot Days(貪心)

script pop his mar side contains fort rgb printf

題目鏈接:http://codeforces.com/problemset/problem/215/D


Description

The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1?≤?i?≤?n) region has a stable temperature of ti degrees in summer.

This summer a group of m

schoolchildren wants to get from the official capital to the cultural capital to visit museums and sights. The trip organizers transport the children between the cities in buses, but sometimes it is very hot. Specifically, if the bus is driving through the i-th region and has k schoolchildren, then the temperature inside the bus is t
i?+?k
degrees.

Of course, nobody likes it when the bus is hot. So, when the bus drives through the i-th region, if it has more than Ti degrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as large as xi rubles and it is charged in each region where the temperature in the bus exceeds the limit.

To save money, the organizers of the trip may arbitrarily add or remove extra buses in the beginning of the trip, and between regions (of course, they need at least one bus to pass any region). The organizers can also arbitrarily sort the children into buses, however, each of buses in the i-th region will cost the organizers costi rubles. Please note that sorting children into buses takes no money.

Your task is to find the minimum number of rubles, which the organizers will have to spend to transport all schoolchildren.

Input

The first input line contains two integers n and m(1?≤?n?≤?105; 1?≤?m?≤?106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next n lines contain four integers each: the i-th line contains ti, Ti, xi and costi (1?≤?ti,?Ti,?xi,?costi?≤?106). The numbers in the lines are separated by single spaces.

Output

Print the only integer — the minimum number of roubles the organizers will have to spend to transport all schoolchildren.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64dspecifier.

Sample Input

Input
2 10
30 35 1 100
20 35 10 10
Output
120
Input
3 100
10 30 1000 1
5 10 1000 3
10 40 1000 100000
Output
200065

Hint

In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal30?+?10?=?40 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won‘t exceed the limit. Overall, the organizers will spend 100?+?10?+?10?=?120 rubles.


題意:(轉)

n個城市,m個人。每個城市有一個溫度t,車內的溫度不能超過T。否者賠償每個人x元,每一輛車的價格c,那個車內的溫度為t加車內的人,對於每個站,你能夠選擇坐計量車,求花費最小?


思路:

對於每個站,假設T<t 那麽一輛車,假設t+m<=T,一輛車,假設t<T可是t+m>T;

那麽就要考慮了

如今如果全部人在一輛車,價格為c+m*x;

假設有一部分人出去租車,發現用的錢少一點。那麽那一輛車最好坐滿,否者空出來的位置能夠坐的人就在那個已經滿的車內須要補償,

假設一部分人出去發現劃算,那麽為什麽在出去一部分呢?這樣想來就僅僅有兩種情況了

1‘ 全部人在一輛車上

2 每一個車都不超過溫度T,可是都坐滿了

代碼例如以下:

#include <cstdio>
#include <cmath>
#define LL __int64
int main()
{
    LL n, m;
    LL t, T, x, c;

    while(~scanf("%I64d%I64d",&n,&m))
    {
        LL sum = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%I64d%I64d%I64d%I64d",&t,&T,&x,&c);
            if(t+m <= T)
            {
                sum+=c;//不必付錢
            }
            else if(t >= T)
            {
                sum+=x*m+c;//必須付錢
            }
            else
            {
                LL tt = T-t;//一個車能坐幾個人
                LL car = m/tt;
                if(m%tt)
                {
                    car++;
                }
                LL minn = x*m+c;//全坐一輛車
                if(minn > car*c)//分開坐
                {
                    minn = car*c;
                }
                sum+=minn;
            }
        }
        printf("%I64d\n",sum);
    }
    return 0;
}


CodeForces 215D Hot Days(貪心)