Hot Days Codeforces Round #132 (Div. 2) 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 ti?+?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 tiTixi 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 1000100

typedef __int64 ll;

ll n,m,t,T,x,c;

int main()
{
	ll ans;
	while(~scanf("%I64d%I64d",&n,&m))
	{
		ans=0;
		while(n--)
		{
			scanf("%I64d%I64d%I64d%I64d",&t,&T,&x,&c);
			if(T<=t)
			{
				ans+=x*m+c;
				continue;
			}
			if(t+m<=T)
			{
				ans+=c;
				continue;
			}
			ll temp=T-t;
			temp=m%temp? m/temp+1:m/temp;
			ans+=min(x*m+c,c*temp);

		}
		printf("%I64d\n",ans);
	}
	 return 0;
}
时间: 2024-10-03 05:05:13

Hot Days Codeforces Round #132 (Div. 2) D(贪心)的相关文章

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

Codeforces Round #546 (Div. 2) D 贪心 + 思维

https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里 题解 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u 假设前面有一个不能和u换位置的集合S,

Hot Days Codeforces Round #132 (Div. 2) 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 sum

Codeforces Round #547 (Div. 3) F 贪心 + 离散化

https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 代码 #include<bits/stdc++.h> #define M 5000005 #define ll long long #define pb push_back using namespace std; struct N{int l,r;N(int l=0,int r=0):l(l)

Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当然最后只是输出cost,就没必要建树什么的了.只要理解了huffman算法构造最优二叉树的思路,就按那么想就知道每个a[i]要加多少次了. 当然这道题没想到这些也可以找出规律的,就是一种贪心思想. #include<iostream> #include<cstdio> #include

Codeforces Round #228 (Div. 1) C 贪心

嘎嘎,今天被一些事耽误了,但是还是A了几个题目,这道题还不错 题目链接: 题意:两个人玩游戏,有N堆纸牌,纸牌上有数字,A每次只能取N堆中的 其中一个的顶部的 纸牌,B只能取N堆中的其中一个底部 的纸牌,A,B都想让自己取的和最大,问最后比分为多少 画了一下,如果某一堆里的 纸牌数量为偶数,发现其实是两个人各分一半,因为如果对方想从这里拿走本来属于自己那半部分的 较大的牌,自己完全来得及阻止的, 接下来就是奇数了,奇数 其实先手者就抢到了中间的一张牌,另外两半还是各自一半,所以 应该以每个奇数堆

Codeforces Round #257 (Div. 2)C 贪心

C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has a big rectangular chocolate bar that consists of n?×?m unit squares. He wants to cut this bar exactly k time

Codeforces Round #263 (Div. 1) A B C

Codeforces Round #263 (Div. 1) A:贪心,排个序,然后从后往前扫一遍,计算后缀和,之后在从左往右扫一遍计算答案 B:树形DP,0表示没有1,1表示有1,0遇到0必然合并,0遇到1也必然合并,1遇到0必然合并,1遇到1,必然切断,按照这样去转移即可 C:树状数组,再利用启发式合并,开一个l,r记录当前被子左右下标,和一个flip表示是否翻转 代码: A: #include <cstdio> #include <cstring> #include <