题目原文: http://codeforces.com/problemset/problem/853/B
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.
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).
题目大意:找出一段至少长为k的区间,让所有人能够乘坐飞机到达,并且能够乘坐飞机离开,并且让总花费最小
解题思路:题意稍稍有些难懂,开始想多了,以为需要动态规划。赛后仔细思考才发现这道题可以转化为一个类似复杂前缀和的问题,使用尺取法维护。
先将所有飞机分成两类,一类是进城,一类是出城
总天数是1e6,所以我们维护出,前 i 天总共有多少个代表可以到达会场,并且维护最小花费,这个是从前向后维护
同理我们再从后向前维护出后 i 天有多少个代表可以离场,并且维护最小花费
这个问题就变成了找到一个长度为k的区间,左端点以前n个代表都可以到达会场,后端点以后n个代表都可以离开会场,for一遍维护最小值即可
AC代码:
/*
* @Author: wchhlbt
* @Last Modified time: 2017-09-14
*/
#include <bits/stdc++.h>
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1000007
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
#define pb push_back
#define _ << " " <<
using namespace std;
typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);
int dx[5] = {0,0,1,-1,0};
int dy[5] = {1,-1,0,0,0};
inline int read(){ int cnt; scanf("%d",&cnt); return cnt;}
int n,m,k;
struct node{
int d,f,c;//d 天数 f 所属城市 c 开销
node(int d = 0, int f = 0, int c = 0) : d(d),f(f),c(c){}
};
vector<node> in,out;
ll incnt[maxn+7],insum[maxn+7],outcnt[maxn+7],outsum[maxn+7],invis[maxn+7],outvis[maxn+7];
//invis数组维护第i座城市的代表是否可以到达会场,如果可以的话维护最小花费
bool cmp(node r, node s)
{
return r.d<s.d;
}
int main()
{
cin>>n>>m>>k;
for(int i = 0; i<m; i++){
int d,f,t,c;
scanf("%d%d%d%d",&d,&f,&t,&c);
if(t==0) in.pb(node(d,f,c));
else out.pb(node(d,t,c));
}
sort(in.begin(),in.end(),cmp);
sort(out.begin(),out.end(),cmp);
for(int i = 1, j = 0; i<=maxn; i++){//维护前缀
insum[i] = insum[i-1];
incnt[i] = incnt[i-1];
while(j<in.size() && in[j].d<=i){
int o = in[j].f;
if(invis[o]==0){
incnt[i]++;
insum[i] += in[j].c;
invis[o] = in[j].c;
}
else if(in[j].c < invis[o]){
insum[i] += (in[j].c - invis[o]);
invis[o] = in[j].c;
}
j++;
}
}
for(int i = maxn,j = out.size()-1; i>=1; i--){//维护后缀
outsum[i] = outsum[i+1];
outcnt[i] = outcnt[i+1];
while(j>=0 && out[j].d>=i){
int o = out[j].f;
if(outvis[o]==0){
outcnt[i]++;
outsum[i] += out[j].c;
outvis[o] = out[j].c;
}
else if(out[j].c < outvis[o]){
outsum[i] += (out[j].c - outvis[o]);
outvis[o] = out[j].c;
}
j--;
}
}
ll ans = 1e18;
for(int i = 1; i<=maxn; i++){
if(i+k+1<=maxn && incnt[i]==n && outcnt[i+k+1]==n)
ans = min(ans,insum[i] + outsum[i+k+1]);
}
if(ans>=1e18) cout << -1 << endl;
else cout << ans << endl;
return 0;
}