codeforces 854D Jury Meeting (思维、尺取)

题目原文: http://codeforces.com/problemset/problem/853/B

B. Jury Meeting

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 ≤ 1050 ≤ m ≤ 1051 ≤ 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 ≤ 1060 ≤ fi ≤ n0 ≤ ti ≤ n1 ≤ 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).

题目大意:找出一段至少长为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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值