zoukankan      html  css  js  c++  java
  • CF Round433 B. Jury Meeting

    题目链接http://codeforces.com/problemset/problem/853/B

    题目大意:自己看吧。。。

    解题思路:刚开始看题解也没看明白,搞了一下午。最后一句话给看到一句话:

    排序后前缀最小花费,后缀最小花费,扫一遍就OK了。

    具体过程:按照day排序之后,记录前缀最小花费以及对应的最大day,记录后缀最小花费以及对应最小day,然后用双指针对前缀的每一个查找后缀中满足条件的,更新ans即可。

    对于前缀,如果j > i, 则 prefix(j) <= prefix(i) , day(j) >= day(i);对于后缀,如果j < i, 则 suffix(j) <= suffix(i), day(j) <= day(i),因此若前缀i对应的后缀为k,则k之前的后缀一定不满足第i+1个前缀(除非prefix(i+1)==prefix(i)相等)。因此可以采用双指针来解决。

    代码:

      1 const ll inf = 1e15;
      2 const int maxn = 1e5 + 5;
      3 struct node{
      4     int d, f, t, c;
      5     bool operator < (const node &t) const{
      6         return d < t.d;
      7     }
      8 }; 
      9 vector<node> fvec, tvec;
     10 ll fc[maxn], tc[maxn];
     11 int fcid[maxn], tcid[maxn];
     12 int cost[maxn];
     13 int n, m, k;
     14 
     15 bool dowork(){
     16     sort(fvec.begin(), fvec.end());
     17     sort(tvec.begin(), tvec.end());
     18     memset(cost, 0, sizeof(cost));
     19     bool flag1 = false, flag2 = false;
     20     int cnt = 0, tmid = 0;
     21     ll tmp = 0;
     22     for(int i = 0; i < fvec.size(); i++){
     23         node u = fvec[i];
     24         if(!cost[u.f]){
     25             cost[u.f] = u.c;
     26             tmp += u.c;
     27             cnt++;
     28             tmid = u.d;
     29         }
     30         else{
     31             if(u.c < cost[u.f]) {
     32                 tmp -= cost[u.f];
     33                 cost[u.f] = u.c;
     34                 tmp += u.c;
     35                 tmid = u.d;
     36             }
     37         }
     38         if(cnt == n) {
     39             fc[i] = tmp;
     40             fcid[i] = tmid;
     41             flag1 = true;
     42         }
     43         else fc[i] = inf;
     44     }
     45     memset(cost, 0, sizeof(cost));
     46     cnt = tmid = 0;
     47     tmp = 0;
     48     for(int j = tvec.size() - 1; j >= 0; j--){
     49         node u = tvec[j];
     50         if(!cost[u.t]){
     51             cost[u.t] = u.c;
     52             tmp += u.c;
     53             tmid = u.d;
     54             cnt++;
     55         }
     56         else{
     57             if(u.c < cost[u.t]){
     58                 tmp -= cost[u.t];
     59                 cost[u.t] = u.c;
     60                 tmp += u.c;
     61                 tmid = u.d;
     62             }
     63         }
     64         if(cnt == n) {
     65             tc[j] = tmp;
     66             tcid[j] = tmid;
     67             flag2 = true;
     68         }
     69         else tc[j] = inf;
     70     }
     71     if(flag1 && flag2) return true;
     72     return false;
     73 }
     74 
     75 void solve(){
     76     if(!dowork()){
     77         puts("-1");
     78         return;
     79     }
     80     int si = 0, sj = 0;
     81     while(fc[si] == inf) si++;
     82     while(tc[sj] == inf) sj--;
     83     ll ans = inf;
     84     while(si < fvec.size()){
     85         int u = fc[si], v = fcid[si];
     86         while(sj < tvec.size() && tc[sj] < inf && tcid[sj] < v + k + 1) sj++;
     87         if(sj >= tvec.size() || tc[sj] == inf) break;
     88         ans = min(ans, fc[si] + tc[sj]);
     89         si++;
     90     }
     91     if(ans == inf) ans = -1;
     92     printf("%lld
    ", ans);
     93 }
     94 int main(){
     95     scanf("%d %d %d", &n, &m, &k);
     96     for(int i = 0; i < m; i++){
     97         node u;
     98         scanf("%d %d %d %d", &u.d, &u.f, &u.t, &u.c);
     99         if(u.f == 0) tvec.push_back(u);
    100         else fvec.push_back(u);
    101     }
    102     solve();
    103 }

    题目:

    B. 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.

  • 相关阅读:
    Webdriver API之元素定位
    学生XX码
    网站设计基础
    JS基础知识
    1、变量和基本类型
    网上地址下载图片
    网上地址下载图片2
    微信账号
    INSERT INTO SELECT语句与SELECT INTO FROM语句
    【基础知识】创建匹配游戏
  • 原文地址:https://www.cnblogs.com/bolderic/p/7491759.html
Copyright © 2011-2022 走看看