Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D. Jury Meeting(双指针模拟)
1 second
512 megabytes
standard input
standard output
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 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
24500
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 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+1个城市,每个城市都有一个人,他们要去0城市参加活动,一起待k天,然后再回来,你可以提前去也可以延后回去,问 你能不能使所有人一起待k天,可以的话,最小花费是多少?
【题解】将航班分为两部分(去和回来),然后找到两个极限位置L,R,去的航班在L之前都没法使得左右人到达,回来的人在R之后 不会全部都回来,然后双指针,维护区间长度>=k,但是要预处理,对于前L,mn[0][i]表示前L个航班从i城市出发到达0城市的最小花费,
然后对于回来的航班,mn[1][i]表示所有从L航班及之前出发的都能回来的航班中回到i城市的最小花费,然后全部加进ans,然后在双指针的过程中不断地更新mn[0][i],mn[1][i],取ans最小值即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
ll qpow(int x,int qq){ll f=,p=x;while(qq){if(qq&)f=f*p%mod;p=1LL*p*p%mod;qq>>=;}}
int n,m,k;
int mn[][N];
bool vis[N];
multiset<int>s[N];
struct man{
int d,f,t,c;
}arrive[N],depart[N];
bool cmp(const man &a,const man &b){return a.d<b.d;};
int main(){
for(int i=;i<N;i++)mn[][i]=mn[][i]=;
int cnt1=,cnt2=,cnt=,L=-,R=-;
scanf("%d%d%d",&n,&m,&k);
while(m--){
int d,f,t,c;
scanf("%d%d%d%d",&d,&f,&t,&c);
if(!f)depart[++cnt2]=man{d,f,t,c};
if(!t)arrive[++cnt1]=man{d,f,t,c};
}
sort(arrive+,arrive++cnt1,cmp);
sort(depart+,depart++cnt2,cmp);
for(int i=;i<=cnt1;i++){
mn[][arrive[i].f]=min(mn[][arrive[i].f],arrive[i].c);
if(!vis[arrive[i].f]){
vis[arrive[i].f]=true;
cnt++;
}
if(cnt==n){
L=i;
break;
} }
int l=L,r;
met(vis,false);cnt=;
for(int i=cnt2;i>=;i--){
if(depart[i].d<arrive[L].d+k+)break;
mn[][depart[i].t]=min(mn[][depart[i].t],depart[i].c);
s[depart[i].t].insert(depart[i].c);
r=i;
if(!vis[depart[i].t]){
vis[depart[i].t]=true;
cnt++;
}
if(cnt==n&&R==-){
R=i;
}
}
ll ans=,res;
for(int i=;i<=n;i++){
ans+=mn[][i]+mn[][i];
}
res=ans;
while(){
if(arrive[l].d+k+>depart[R].d||r>R||l>=cnt1)break;
while(arrive[l].d+k+<=depart[r].d&&l<cnt1){
l++;
if(arrive[l].c<mn[][arrive[l].f]){
ans-=(mn[][arrive[l].f]-arrive[l].c);
mn[][arrive[l].f]=arrive[l].c;
}
if(arrive[l].d+k+<=depart[r].d)res=min(res,ans);
} while(depart[r].d<arrive[l].d++k&&r<R){
r++;
s[depart[r-].t].erase(s[depart[r-].t].find(depart[r-].c));
ans-=(mn[][depart[r-].t]-*s[depart[r-].t].begin());
mn[][depart[r-].t]=*s[depart[r-].t].begin();
if(arrive[l].d+k+<=depart[r].d)res=min(res,ans);
}
}
if(L==-||R==-)puts("-1");
else if(depart[R].d-arrive[L].d>=k+)printf("%lld\n",res);
else puts("-1");
return ;
}
Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D. Jury Meeting(双指针模拟)的更多相关文章
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)
		
A. Fraction 题目链接:http://codeforces.com/contest/854/problem/A 题目意思:给出一个数n,求两个数a+b=n,且a/b不可约分,如果存在多组满足 ...
 - Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D
		
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the ...
 - Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C
		
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
 - Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) B
		
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartme ...
 - Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) A
		
Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned tha ...
 - Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) D mt19937
		
https://codeforces.com/contest/1040/problem/D 用法 mt19937 g(种子); //种子:time(0) mt19937_64 g(); //long ...
 - 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...
 - 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...
 - Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed
		
地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...
 
随机推荐
- 51Nod 1095 Anigram单词 | Hash
			
Input示例 5 add dad bad cad did 3 add cac dda Output示例 1 0 2 题意:一系列字符串,查询字符串S,能通过其他字符串交换串内字符顺序得到的字符串个数 ...
 - 【poj2114】点分治(离线)
			
boatherds 2s 64M by czy 求一颗树上距离为K的点对是否存在 输入数据 n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行每行询问一个K 输出数据 对于每 ...
 - 【POJ】1830 开关问题(高斯消元)
			
http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...
 - PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)
			
题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...
 - base--AuditResult
			
//参考base-4.0.2.jar public class AuditResult implements TimeReferable, Serializable //参考api-1.0.0.jar ...
 - Intel MKL(Math Kernel Library)
			
1.Intel MKL简介 Intel数学核心函数库(MKL)是一套高度优化.线程安全的数学例程.函数,面向高性能的工程.科学与财务应用.英特尔 MKL 的集群版本包括 ScaLAPACK 与分布式内 ...
 - Part2-HttpClient官方教程-Chapter1-基础
			
前言 超文本传输协议(HTTP)可能是当今Internet上使用的最重要的协议.Web服务.网络支持的设备和网络计算的增长继续扩展了HTTP协议在用户驱动的Web浏览器之外的作用,同时增加了需要HTT ...
 - ssh日常优化使用
			
config文件的使用 ssh命令默认会加载 ~/.ssh/config 文件作为配置文件,如果没有则采用默认配置.如果我们想要对ssh进行定制,那么就可以使用如下方法 [root@linux-nod ...
 - Django【进阶】分页功能Pagination
			
项目中,我们需要很多非业务逻辑的功能,例如分页功能,而且此类功能移植性很好,可以在不同的项目中使用,所以整理好这些功能会一定程度上提高开发效率,下面是分页功能代码,使用时,可单独放在utils目录 & ...
 - humble_USACO
			
Humble Numbers For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all num ...