結果

問題 No.1014 competitive fighting
ユーザー snow39snow39
提出日時 2020-03-21 12:53:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 3,186 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 119,200 KB
実行使用メモリ 71,516 KB
最終ジャッジ日時 2023-09-21 19:23:53
合計ジャッジ時間 9,771 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 208 ms
46,172 KB
testcase_06 AC 191 ms
46,288 KB
testcase_07 AC 194 ms
46,292 KB
testcase_08 AC 197 ms
46,396 KB
testcase_09 AC 198 ms
46,356 KB
testcase_10 AC 112 ms
46,540 KB
testcase_11 AC 155 ms
71,516 KB
testcase_12 AC 200 ms
46,380 KB
testcase_13 AC 84 ms
23,144 KB
testcase_14 AC 75 ms
21,596 KB
testcase_15 AC 93 ms
25,292 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 32 ms
10,848 KB
testcase_18 AC 158 ms
37,932 KB
testcase_19 AC 192 ms
45,048 KB
testcase_20 AC 60 ms
17,720 KB
testcase_21 AC 142 ms
34,616 KB
testcase_22 AC 110 ms
29,320 KB
testcase_23 AC 41 ms
13,264 KB
testcase_24 AC 42 ms
13,712 KB
testcase_25 AC 10 ms
5,740 KB
testcase_26 AC 137 ms
34,284 KB
testcase_27 AC 32 ms
10,860 KB
testcase_28 AC 36 ms
12,004 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 197 ms
44,996 KB
testcase_31 AC 147 ms
35,500 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 12 ms
6,944 KB
testcase_34 AC 136 ms
52,672 KB
testcase_35 AC 185 ms
63,008 KB
testcase_36 AC 118 ms
48,304 KB
testcase_37 AC 6 ms
4,796 KB
testcase_38 AC 109 ms
43,456 KB
testcase_39 AC 55 ms
22,504 KB
testcase_40 AC 105 ms
43,412 KB
testcase_41 AC 74 ms
31,340 KB
testcase_42 AC 169 ms
66,644 KB
testcase_43 AC 11 ms
6,524 KB
testcase_44 AC 153 ms
60,708 KB
testcase_45 AC 92 ms
34,224 KB
testcase_46 AC 17 ms
9,140 KB
testcase_47 AC 51 ms
22,048 KB
testcase_48 AC 126 ms
45,756 KB
testcase_49 AC 6 ms
5,016 KB
testcase_50 AC 166 ms
63,960 KB
testcase_51 AC 94 ms
38,432 KB
testcase_52 AC 7 ms
5,252 KB
testcase_53 AC 203 ms
46,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

/* example
    int n=scc.scc();
    scc.build(n,g);
    (g:outside)
*/
struct StronglyConnectedComponents{
    vector<int> used,cmp,vs;
    vector<vector<int>> ori;
    vector<vector<int>> v,rv;
    StronglyConnectedComponents(int n):used(n),cmp(n,-1),v(n),rv(n){}
 
    void add_edge(int x,int y){
        v[x].push_back(y);
        rv[y].push_back(x);
    }
    void dfs(int x){
        used[x]=true;
        for(auto to:v[x]) if(!used[to]) dfs(to);
        vs.push_back(x);
    }
 
    void rdfs(int x,int k){
        cmp[x]=k;
        for(auto to:rv[x]) if(cmp[to]==-1) rdfs(to,k);
    }
 
    int scc(){
        for(int i=0;i<used.size();i++) if(!used[i]) dfs(i);
        reverse(vs.begin(),vs.end());
        int k=0;
        for(auto x:vs) if(cmp[x]==-1) rdfs(x,k++);

        ori.resize(k);
        for(int i=0;i<used.size();i++) ori[cmp[i]].push_back(i);

        return k;//k:size
    }

    void build(int k,vector<vector<int>> &g){
        g.resize(k);
        for(int i=0;i<v.size();i++){
            for(auto to:v[i]){
                int x=cmp[i],y=cmp[to];
                if(x!=y) g[x].push_back(y);
            }
        }
    }
};


int N;
vector<vector<int>> g;
vector<ll> A,B,C;
vector<ll> V;
vector<ll> dp;

const ll INF=1e18;
vector<vector<int>> ori;

void dfs(int now,int par){
  if(dp[now]!=0) return;
  ll res=0;
  for(auto nex:g[now]){
    if(nex==par) continue;
    dfs(nex,now);
    if(dp[nex]==-INF){
      dp[now]=-INF;
      return;
    }
    chmax(res,dp[nex]);
  }
  dp[now]=V[now]+res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin>>N;
  A.resize(N);
  B.resize(N);
  C.resize(N);
  rep(i,N) cin>>A[i]>>B[i]>>C[i];

  vector<int> ord(N);
  rep(i,N) ord[i]=i;
  sort(ord.begin(),ord.end(),[&](int a,int b){
    return A[a]<A[b];
  });

  vector<ll> w=A;
  sort(w.begin(),w.end());
  g.resize(2*N);
  for(int i=0;i<N;i++){
    int now=ord[i];
    ll lim=B[now]-C[now];
    int z=upper_bound(w.begin(),w.end(),lim)-w.begin();
    if(z>0) g[now].push_back(N+z-1);
  }
  for(int i=N;i<2*N;i++){
    if(i+1<2*N) g[i+1].push_back(i);
    g[i].push_back(ord[i-N]);
  }

  StronglyConnectedComponents scc(2*N);
  for(int i=0;i<2*N;i++){
    for(auto to:g[i]) scc.add_edge(i,to);
  }
  int M=scc.scc();
  g.clear();
  scc.build(M,g);
  ori=scc.ori;

  vector<int> num(M,0);
  rep(i,N) num[scc.cmp[i]]++;

  V.resize(M,0);  
  dp.resize(M,0);
  for(int i=0;i<M;i++) for(auto p:ori[i]) if(p<N) V[i]+=B[p];
  for(int i=0;i<M;i++) if(num[i]>1){
    V[i]=-INF;
    dp[i]=-INF;
  }

  rep(i,M) if(dp[i]==0) dfs(i,-1);

  vector<ll> ans(N,0);
  rep(i,M) for(auto p:ori[i]) if(p<N) ans[p]=dp[i];
  rep(i,N){
    if(ans[i]==-INF) cout<<"BAN"<<"\n";
    else cout<<ans[i]<<"\n";
  }

  return 0;
}
0