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 input
output
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.

Input

The first line of input contains three integers nm 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 difiti and ci (1 ≤ di ≤ 106, 0 ≤ 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).

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+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+50;;
const int M = 17;
const int mod = 19260817;
const int mo=123;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
ll qpow(int x,int qq){ll f=1,p=x;while(qq){if(qq&1)f=f*p%mod;p=1LL*p*p%mod;qq>>=1;}}
int n,m,k;
int mn[2][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=1;i<N;i++)mn[0][i]=mn[1][i]=10000000;
    int cnt1=0,cnt2=0,cnt=0,L=-1,R=-1;
    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+1,arrive+1+cnt1,cmp);
    sort(depart+1,depart+1+cnt2,cmp);
    for(int i=1;i<=cnt1;i++){
        mn[0][arrive[i].f]=min(mn[0][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=0;
    for(int i=cnt2;i>=1;i--){
        if(depart[i].d<arrive[L].d+k+1)break;
        mn[1][depart[i].t]=min(mn[1][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==-1){
            R=i;
        }
    }
    ll ans=0,res;
    for(int i=1;i<=n;i++){
        ans+=mn[0][i]+mn[1][i];
    }
    res=ans;
    while(1){
        if(arrive[l].d+k+1>depart[R].d||r>R||l>=cnt1)break;
        while(arrive[l].d+k+1<=depart[r].d&&l<cnt1){
            l++;
            if(arrive[l].c<mn[0][arrive[l].f]){
                ans-=(mn[0][arrive[l].f]-arrive[l].c);
                mn[0][arrive[l].f]=arrive[l].c;
            }
            if(arrive[l].d+k+1<=depart[r].d)res=min(res,ans);
        }

        while(depart[r].d<arrive[l].d+1+k&&r<R){
            r++;
            s[depart[r-1].t].erase(s[depart[r-1].t].find(depart[r-1].c));
            ans-=(mn[1][depart[r-1].t]-*s[depart[r-1].t].begin());
            mn[1][depart[r-1].t]=*s[depart[r-1].t].begin();
            if(arrive[l].d+k+1<=depart[r].d)res=min(res,ans);
        }
    }
    if(L==-1||R==-1)puts("-1");
    else if(depart[R].d-arrive[L].d>=k+1)printf("%lld
",res);
    else puts("-1");
    return 0;
}
原文地址:https://www.cnblogs.com/jianrenfang/p/7491941.html