Codeforces 854D Jury Meeting【思维+前后缀和+二分】
来源:互联网 发布:java 股票指标算法库 编辑:程序博客网 时间:2024/06/05 10:29
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 kdays 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.
The first line of input contains three integers n, m 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 di, fi, ti 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 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).
2 6 51 1 0 50003 2 0 55002 2 0 600015 0 2 90009 0 1 70008 0 2 6500
24500
2 4 51 2 0 50002 1 0 45002 1 0 30008 0 1 6000
-1
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个人在N个城市中,编号为1~N,现在我们要聚集这些人到首都0号城市, 一起用K天来做一个工作,我们现在给出M个航班,每个航班四个信息,出发的时间,出发的是城市,到达的城市以及航班的花费。
我们现在要求这N个人一起到城市0然后工作完之后都回家的最小花费。
思路:
①首先我们将M个航班按照时间从小到大排序一下,这样在前边的就是出发时间早的,否则就是出发时间晚的。
②那么我们不难想到,O(m)枚举一个聚集到一起的一个时间点,那么这个时间之前我们的任务就是将这N个人飞到首都去,这里每个人的问题都是单独的,所以从当前枚举的聚集点之前的部分,我们只要维护每个人到达首都的最小花费即可,然后将其加和,维护一个前缀和,设定为L【i】,表示以第i个航班结束为止聚集到一起的最小花费,对应反过去搞,就是R【i】,表示以第i天为结束工作的日子,之后将所有人返回各个城市的最小花费。
③那么我们O(m)枚举这样的断点,然后二分回家的那个点维护一个最小权值和即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64struct node{ ll d,from,to,val;} a[250000];ll vis[250000];ll L[250000];ll R[250000];ll cmp(node a,node b){ return a.d<b.d;}int main(){ ll n,m,k; while(~scanf("%I64d%I64d%I64d",&n,&m,&k)) { memset(vis,0,sizeof(vis)); memset(L,-1,sizeof(L)); memset(R,-1,sizeof(R)); for(ll i=1; i<=m; i++)scanf("%I64d%I64d%I64d%I64d",&a[i].d,&a[i].from,&a[i].to,&a[i].val); sort(a+1,a+1+m,cmp); ll sum=0; ll tot=0; for(ll i=1; i<=m; i++) { if(a[i].from==0)continue; if(vis[a[i].from]==0) { tot+=a[i].val; sum++; vis[a[i].from]=a[i].val; } else { if(vis[a[i].from]>a[i].val) { tot-=vis[a[i].from]; tot+=a[i].val; vis[a[i].from]=a[i].val; } } if(sum==n)L[i]=tot; } sum=0; tot=0; memset(vis,0,sizeof(vis)); for(ll i=m; i>=1; i--) { if(a[i].from!=0)continue; if(vis[a[i].to]==0) { tot+=a[i].val; sum++; vis[a[i].to]=a[i].val; } else { if(vis[a[i].to]>a[i].val) { tot-=vis[a[i].to]; tot+=a[i].val; vis[a[i].to]=a[i].val; } } if(sum==n)R[i]=tot; } ll zz=1000000000000000000; for(ll i=m;i>=1;i--) { if(R[i]==-1)continue; else { if(R[i+1]!=-1) R[i]=min(R[i],R[i+1]); } } for(ll i=1;i<=m;i++) { if(L[i]==-1)continue; else { ll pos=-1; ll day=a[i].d+k+1; ll l=i+1; ll r=m; while(r-l>=0) { ll mid=(l+r)/2; if(a[mid].d>=day) { r=mid-1; pos=mid; } else l=mid+1; } if(pos==-1)continue; else if(R[pos]!=-1)zz=min(zz,R[pos]+L[i]); } } if(zz==1000000000000000000)printf("-1\n"); else printf("%I64d\n",zz); }}
- Codeforces 854D Jury Meeting【思维+前后缀和+二分】
- [codeforces] 854D. Jury Meeting(前/后缀最小值)
- Codeforces 854 D Jury Meeting(前缀和后缀)
- Codeforces Round #433 (Div. 2) D. Jury Meeting(思维 贪心 前后缀)
- codeforces 854D Jury Meeting (思维、尺取)
- codeforces 854 D. Jury Meeting(dp水题)
- Codeforces 854 D. Jury Meeting(技巧)
- Codeforces Round #433 (Div. 2 D. Jury Meeting 二分
- Codeforces 651D Image Preview【思维+二分+前缀后缀和】
- Codeforces Round #433 D. Jury Meeting
- codeforces 845D Jury Meeting(div.2)
- Codeforces Round #433 D. Jury Meeting
- CF 845D Jury Meeting 枚举断点+前缀后缀
- Codeforces Round #433 (Div. 1) B Jury Meeting(思维)
- Jury Meeting CodeForces
- Codeforces Round #433Div. 2 D Jury Meeting 优先队列
- Codeforces Round #433 (Div. 2) D. Jury Meeting
- Codeforces Round #433 (Div. 2) D. Jury Meeting
- 如何u盘安装Linux系统CentOS7.2
- eclipse 创建maven 项目 动态web工程完整示例
- javassist
- https://github.com/fendouai/Awesome-Chatbot
- LeetCode #7 Reverse Integer
- Codeforces 854D Jury Meeting【思维+前后缀和+二分】
- sql with as 用法
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup
- sass+compass异常:error screen.scss (Line 3: File to import not found or unreadable: blueprint/reset.)
- 链表 c++
- linux启动XAMPP命令
- kafka--简介、组件构成、安装、基础操作
- poj1062 [ZJOI2002]昂贵的聘礼(spfa+枚举)
- Linux