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 *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 *d**i*, *f**i*, *t**i* and *c**i* (1 ≤ *d**i* ≤ 106, 0 ≤ *f**i* ≤ *n*, 0 ≤ *t**i* ≤ *n*, 1 ≤ *c**i* ≤ 106, exactly one of *f**i* and *t**i* 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 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
```
Output
```
24500
```
Input
```
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 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。
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 2000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
int time;
int f;
int t;
ll cost;
} a[maxn];
int n;
int m;
int k;
bool cmp(node aa, node bb)
{
return aa.time < bb.time;
}
int incnt = 0;
int outcnt = 0;
ll in[maxn];
ll out[maxn];
ll sumin[maxn];
ll sumout[maxn];
ll sum1 = 0ll;
ll sum2 = 0ll;
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\common_text\code_stream\out.txt","w",stdout);
gbtb;
cin >> n >> m >> k;
ll mtime = 0;
repd(i, 1, m)
{
cin >> a[i].time >> a[i].f >> a[i].t >> a[i].cost;
mtime = max(mtime, 1ll * a[i].time);
}
sort(a + 1, a + 1 + m, cmp);
repd(i, 1, m)
{
if (a[i].t == 0)
{
if (in[a[i].f] == 0)
{
incnt++;
in[a[i].f] = a[i].cost;
sum1 += a[i].cost;
} else
{
if (in[a[i].f] > a[i].cost)
{
sum1 -= in[a[i].f];
sum1 += a[i].cost;
in[a[i].f] = a[i].cost;
}
}
if (incnt == n)
{
sumin[a[i].time] = sum1;
}
}
}
for (int i = m; i >= 1; i--)
{
if (a[i].f == 0)
{
if (out[a[i].t] == 0)
{
outcnt++;
out[a[i].t] = a[i].cost;
sum2 += a[i].cost;
} else
{
if (out[a[i].t] > a[i].cost)
{
sum2 -= out[a[i].t];
sum2 += a[i].cost;
out[a[i].t] = a[i].cost;
}
}
if (outcnt == n)
{
sumout[a[i].time] = sum2;
}
}
}
ll ans = 11731173111173111;
repd(i, 1, mtime)
{
if (sumin[i] == 0)
sumin[i] = sumin[i - 1];
else if (sumin[i - 1])
sumin[i] = min(sumin[i], sumin[i - 1]);
}
for (int i = mtime; i >= 1; i--)
{
if (sumout[i] == 0)
{
sumout[i] = sumout[i + 1];
}
else if (sumout[i + 1])
sumout[i] = min(sumout[i], sumout[i + 1]);
}
repd(i, 1, mtime - k - 1)
{
if (sumout[i + k + 1] && sumin[i])
ans = min(ans, sumout[i + k + 1] + sumin[i]);
}
if (ans != 11731173111173111)
cout << ans << endl;
else
cout << -1 << endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
```
Jury Meeting CodeForces - 854D (前缀和维护)的更多相关文章
- Jury Meeting CodeForces - 854D
Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体 ...
- codeforces 853b//Jury Meeting// Codeforces Round #433 (Div. 1)
题意:几个人要去一个城市k天,现给出各航班的日期和花费,让这n个人能相会k天的最小花费? 用数组arr1[i]记录在第i天人到齐的最小花费.arr2[i]记录第i天之后才有人开始走的最小花费.然后取a ...
- Codeforces 853B Jury Meeting (差分+前缀和)
<题目链接> 题目大意: 有$ n(n<=1e5)$个城市和一个首都(0号城市),现在每个城市有一个人,总共有$ m (m<=1e5)$次航班,每个航班要么从首都起飞,要么飞到 ...
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D. Jury Meeting(双指针模拟)
D. Jury Meeting time limit per test 1 second memory limit per test 512 megabytes input standard inpu ...
- cf 853 B Jury Meeting [前缀和]
题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首 ...
- 【Codeforces Round #433 (Div. 1) B】Jury Meeting
[链接]h在这里写链接 [题意] 有n个人,它们都要在某一时刻开始,全都到达0位置,然后维持最少k个时间单位,然后再全都回到原来的位置; 第i个人初始的位置是i. 且一共有m班航班. 每一班航班,要么 ...
- Codeforces 853B Jury Meeting
题意 从城市1-n来的评审团到城市0商讨国家大事,离开和抵达的那一天不能讨论,飞机均当天抵达,给出所有飞机起飞抵达代价情况,问能否使所有评审员聚齐连续k天并返回,并求最小代价 思路 从前向后扫一遍,求 ...
- codeforces round 433 D. Jury Meeting
题目大意: 输入n,m,k,分别代表城市的数量,城市编号1~n,航班的数量以及会议必须所有人员到会一起商议的天数,然后及时输入m行航班的信息,每一行输入d,f,t,c分别表示航班到站和始发的那一天(始 ...
- hdu_5776_sum(前缀和维护)
题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和为m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区 ...
随机推荐
- AspNet mvc的一个bug
[HttpPost] public ActionResult updateLoan(TuWenMilitaryRank entity) 使用mvc绑定表单 每次绑定的对象都为null,查看Reques ...
- Windows平台搭建-----C语言
上期我们已经进行Linux的平台搭建,今期我们就来搭建下我们最常用的.最适合初学者的一种方式,那就是搭建Windows平台开发环境,只需要两种工具即可,一个就是编辑器(编辑代码的工具),另一个就是编译 ...
- LeetCode算法题-Nim Game(Java实现)
这是悦乐书的第203次更新,第213篇原创 01 看题和准备 你和你的朋友正在玩下面的Nim游戏:桌子上有一堆石头,每次你轮流去除1到3块石头. 移除最后一块石头的人将成为赢家. 你是第一个取出石块的 ...
- MATLAB中冒号的用法
MATLAB中冒号的用法 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ matlab中冒号代表步长,用实例来说明 >> A=[1 2 3 ...
- webpack模块定义和使用的模式
在使用webpack作为模块加载工具时,我在想module.exports的模块应该是一种什么模式,最直接地思考是单例.不太确定,所以写一个简单例子做测试. 测试代码 singleton.js: va ...
- 解决:Vue刷新/载入页面,出现双括号闪现后消失
https://cn.vuejs.org/v2/api/#v-cloak v-cloak 不需要表达式 用法: 这个指令保持在元素上直到关联实例结束编译.和 CSS 规则如 [v-cloak] { d ...
- postgreSQL 应用case when的例子
selectname,md5(indvl_id_nbr) as indvl_id_nbr,case when char_length(indvl_id_nbr)=18 or char_length(i ...
- MySQL高级知识(十二)——全局查询日志
前言:全局查询日志用于保存所有的sql执行记录,该功能主要用于测试环境,在生产环境中永远不要开启该功能. 1.如何开启 #1.通过my.cnf配置开启该功能. 注:对my.cnf文件配置后,需重启my ...
- BSOJ 5603 -- 【SNOI2017】炸弹
题好数据水系列,网上的十几行神仙解法A了原数据. 这道题要用到线段数优化建图的知识.然而考试考到这道题时我还不会. 我们设分别表示每个炸弹向左和向右最远能炸到哪个炸弹.很容易想到一个思路,就是每个炸弹 ...
- C#事件の事件聚合器(二)
WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...