Jury Meeting CodeForces - 854D (前缀和维护)

Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.

There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.

You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.

Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

Input

The first line of input contains three integers nm and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106).

The i-th of the following m lines contains the description of the i-th flight defined by four integers difiti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output

Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.

If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).

Examples

Input

2 6 51 1 0 50003 2 0 55002 2 0 600015 0 2 90009 0 1 70008 0 2 6500

Output

24500

Input

2 4 51 2 0 50002 1 0 45002 1 0 30008 0 1 6000

Output

-1

Note

The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.

In the second sample it is impossible to send jury member from city 2 back home from Metropolis.

题意:

给你N个大使,M个航班,和一个天数K,

对于每一个航班i,有四个信息,分别是日期,起始站,目标站,价格。

其中起始站和目标站一定有一个是0节点。

让你把1~n个大使都从第i个城市接到0节点,开K天及以上的的会议,然后再全部送回他们的城市。

使之花费的成本最小。

思路:

读入的时候找出最大的天数maxtime。

从1到maxtime维护一个全部人可以送到0城市的最小消费

然后从maxtime到1反向维护一个把全部人送回去的最小消费。

然后1到maxtime 取一个min既是ans。

原文地址:https://www.cnblogs.com/qieqiemin/p/10660555.html

时间: 2024-10-04 09:23:11

Jury Meeting CodeForces - 854D (前缀和维护)的相关文章

Jury Meeting CodeForces - 854D

Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体:首先,将航班分为飞入0和飞出0两类. 然后,枚举会议开始的时间p. 那么,飞入0的航班只有时间<p的生效,飞出0的航班只有时间>p+k-1的生效. 显然,在p变为p+1时,最多只有各一班航班生效与失效. (听说还能二分,但是已经打了100行了,不敢再加了...好累) 1 #include<

CF Round433 B. Jury Meeting

题目链接:http://codeforces.com/problemset/problem/853/B 题目大意:自己看吧... 解题思路:刚开始看题解也没看明白,搞了一下午.最后一句话给看到一句话: 排序后前缀最小花费,后缀最小花费,扫一遍就OK了. 具体过程:按照day排序之后,记录前缀最小花费以及对应的最大day,记录后缀最小花费以及对应最小day,然后用双指针对前缀的每一个查找后缀中满足条件的,更新ans即可. 对于前缀,如果j > i, 则 prefix(j) <= prefix(i

CH0805 前缀和维护奇偶 二分答案

题面 描述 lsp 学习数学竞赛的时候受尽了同仁们的鄙视,终于有一天......受尽屈辱的 lsp 黑化成为了黑暗英雄Lord lsp.就如同中二漫画的情节一样,Lord lsp 打算毁掉这个世界.数学竞赛界的精英 lqr 打算阻止Lord lsp 的阴谋,于是 她集合了一支由数学 竞赛选手组成的超级行动队.由于队员们个个都智商超群,很快,行动队便来到了 Lord lsp 的黑暗城堡的下方. 但是,同样强大的 Lord lsp 在城堡周围布置了一条"不可越过"的坚固防线.防线由很 多防

cf 853 B Jury Meeting [前缀和]

题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首都的飞机的最小后缀花费 都计算出来以后,对于每一个开始开会的时间t,用pre[t-1]+suf[t+k]来更新答案即可 Code: 到处都要用long long...... 最后我只好ctrl+R,替换int为longlong了...... 1 #include<iostream> 2 #incl

Codeforces 853B Jury Meeting

题意 从城市1-n来的评审团到城市0商讨国家大事,离开和抵达的那一天不能讨论,飞机均当天抵达,给出所有飞机起飞抵达代价情况,问能否使所有评审员聚齐连续k天并返回,并求最小代价 思路 从前向后扫一遍,求每天的出发最小代价L[i],从后向前扫,求每天最小离开代价R[i] 从前向后扫一遍,每天的最小代价为L[i]+R[i+k+1] 将每天的默认大小设为1e12,因为最大代价不超过1e11,可以据此确定答案是否合法 代码 #include<bits/stdc++.h> using namespace

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

hdu_5776_sum(前缀和维护)

题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和胃m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区间的和为m的倍数 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 5 const int N=1e5+7; 6 int a[N],vis[5555],t,n

Codeforces 612D 前缀和处理区间问题

传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri],找出被包含了至少k次的点,并求由这些点组成的连续区间的数目,并使该数目最小.输出该数目并将区间从左到右(x的正方向)输出. 比如样例1,给出区间[0,5],[-3,2],[3,8],那么被覆盖了至少两次的区间就是[0,2],[3,5],有两个. 解题思路: 处理区间覆盖,一上来就想到了前缀和,普

codeforces 587C:(LCA倍增+维护最小值)

一开始直接无脑tarjan,回溯只能一层层往上走,太慢了,加了各种优化还是TLE 后来了解到LCA倍增法(在线).复杂度其实相比LCA转RMQ以及tarjan是要稍差一些,但是其中能同步维护的只有LCA倍增,很神奇的算法 #include"cstdio" #include"queue" #include"cmath" #include"stack" #include"iostream" #include&q