codeforces 854 D. Jury Meeting(dp水题)

题目链接

D. Jury Meeting

分析

昨晚打正式赛的时候太急了,代码写的有点毒,下了课后一看,立马发现bug…..

将去的航班表和返回航班表分别按照时间最小和最大排序,对于去的航班来说,挨个扫描过去,对每一个人维护当前的最小花费,若时间总的航班满足等于  n 说明这一天可以作为起始时间,更新这一天的花费 dp[d]=now_ans , now_ans 记录当前最小花费,这个更新是 O(1) d的具体看代码.
对与返程航班也这样干就行了,计算出可以作为返回时间点的最小花费 dp1 .
对两个dp数组分别求到目前为止的最小花费,如 dp[i]=min(dp[i],dp[i1]) i 天出发的最小花费,反向数组亦然.

ac code

#include
#define pb push_back
#define mp make_pair
#define PI acos(-1)
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define ms(x,v) memset((x),(v),sizeof(x))
#define scint(x) scanf("%d",&x );
#define scf(x) scanf("%lf",&x );
#define eps 1e-10
#define dcmp(x) (fabs(x) < eps? 0:((x) <0?-1:1))
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long LL;
typedef long double DB;
typedef pair<int,int> Pair;
const int maxn = 1e6+10;
const int MAX_V= 500+10;
const int MOD = 998244353;

struct day{
    int d,cost,num;
    bool operator< (const day& o)const{
        if(d != o.d)return d > o.d;
        else return cost > o.cost;
    }
};
day P[maxn],b[maxn];

bool cmp(const day & a,const day & b){
    if(a.d != b.d)return a.d >b.d;
    else return a.cost < b.cost;
}

int c[maxn],d[maxn];
LL dp[maxn],dp1[maxn];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,k;
    cin>>n>>m>>k;
    int t1 =0;
    int t2 =0;
    for(int i=1 ; i0,dp1[i] = INF64,d[i]=0;
    priority_queue Q;
    for(int i=1 ; i<=m ; ++i){
        int d,u,v,cost;cin>>d>>u>>v>>cost;
        if(u)Q.push(day{d,cost,u});
        else P[t2++] = day{d,cost,v};
    }
    sort(P,P+t2,cmp);
    int tot =0;
    LL ans =0;
    while (!Q.empty()) {
        day p = Q.top();Q.pop();
        int u = p.num;
        if(!c[u])c[u] = p.cost,tot++,ans += c[u];
        else if( c[u]> p.cost) ans += p.cost - c[u],c[u] = p.cost;
        if(tot == n)dp[p.d] = min(dp[p.d],ans);
    }
    tot =0;
    ans =0;
    for(int i = 0 ; iint v = p.num;
        if(!d[v])d[v] = p.cost,tot++,ans += d[v];
        else if(d[v] > p.cost) ans += p.cost - d[v],d[v] = p.cost;
        if(tot == n)dp1[p.d] = min(dp1[p.d],ans);
    }
    for(int i = 2 ; i1]);
    //std::cout << maxn << '\n';
    for(int i = maxn -2 ; i>=0 ; --i){
        dp1[i] = min(dp1[i],dp1[i+1]);
    }
    ans = INF64;
    for(int i=1 ; i1; ++i){
        ans = min(ans , dp[i]+ dp1[i+k+1]);
    }
    // for(int i=1 ; i<20 ; ++i){
    //     std::cout << dp[i]<<" " << dp1[i] << '\n';
    // }
    if(ans == INF64)std::cout << -1 << '\n';
    else std::cout << ans << '\n';
    //std::cout << "time "<< clock()/1000 <<"ms"<< '\n';
    return 0;
}

你可能感兴趣的:(算法刷题)