답안 #1080818

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1080818 2024-08-29T14:33:43 Z 8pete8 Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
260 ms 120612 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30,valbound=1e9;
//#undef int
int n,k,m,q;
void setIO(string name){		
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);	
}
int go[mxn+10],val[mxn+10],cost[mxn+10],p[mxn+10],deg[mxn+10];
vector<int>adj[mxn+10],have[mxn+10];
int vis[mxn+10],pa[mxn+10],on[mxn+10];;
stack<int>st;
int find(int u){return (pa[u]==u)?u:pa[u]=find(pa[u]);}
void merg(int a,int b){
    a=find(a),b=find(b);
    if(a==b)return;
    pa[a]=b;
}
void dfs(int cur){
    vis[cur]=on[cur]=1;
    st.push(cur);
    if(!vis[go[cur]])dfs(go[cur]);
    else if(on[go[cur]]){
        while(!st.empty()&&st.top()!=go[cur]){
            merg(cur,st.top());
            st.pop();
        }
        if(!st.empty()){
            merg(cur,st.top());
            st.pop();
        }
    }
    on[cur]=0;
}
map<int,int>dp[mxn+10];
//keep prefix sum
//like magic tree?
//can also do merge segtree
void solve(int cur,int root){
    for(auto i:adj[cur]){
        solve(i,0);
        if(dp[i].size()>dp[cur].size())swap(dp[cur],dp[i]);
        for(auto j:dp[i])dp[cur][j.f]+=j.s;
    }
    if(!root){
        dp[cur][val[cur]]+=cost[cur];
        auto it=(dp[cur].find(val[cur]));
        if(it!=dp[cur].begin()){
            it=prev(it);
            while(cost[cur]){
                if(cost[cur]>=it->s){
                    cost[cur]-=it->s;
                    it=dp[cur].erase(it);
                    if(it==dp[cur].begin())break;
                    it=prev(it);
                }
                else it->s-=cost[cur],cost[cur]=0;
            }
        }
    }
}
int sum=0;
int32_t main(){
    fastio
    cin>>n;
    for(int i=1;i<=n;i++)cin>>go[i]>>val[i]>>cost[i],pa[i]=i,sum+=cost[i];
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            dfs(i);
            while(!st.empty())st.pop();
        }
    }
    for(int i=1;i<=n;i++)have[find(i)].pb(i);
    for(int i=1;i<=n;i++)if(find(go[i])!=find(i)){
        adj[find(go[i])].pb(find(i));
        deg[find(i)]++;
    }
    int ans=0;
    for(int i=1;i<=n;i++)if(find(i)==i&&deg[i]==0){
        solve(i,1);
        map<int,int>cnt;
        for(auto j:have[i])cnt[val[j]]+=cost[j];
        int mx=0,cursum=0,can=1;
        auto it=(dp[i].end());
        if(it==dp[i].begin())can=0;
        else it=prev(it);
        auto j=prev(cnt.end());
        while(1){
            while(can&&it->f>=j->f){
                cursum+=it->s;
                if(it==dp[i].begin())can=0;
                it--;
            }
            mx=max(mx,cursum+j->s);
            if(j==cnt.begin())break;
            j--;
        }
        while(can){
            cursum+=it->s;
            if(it==dp[i].begin())can=0;
            it--;
        }
        mx=max(mx,cursum);
        ans+=mx;
    }
    cout<<sum-ans<<'\n';
}
/*
create graph
one cycle per comp
will be a line with cycle at the end
if we have a cycle then every node of that cycle should have the same value
we will have a forest
then we can just get min cost decreasing sequence
*/

Compilation message

worst_reporter2.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
   32 | #pragma GCC optimize ("03,unroll-lopps")
      |                                        ^
worst_reporter2.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   38 | void setIO(string name){
      |                       ^
worst_reporter2.cpp:47:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   47 | int find(int u){return (pa[u]==u)?u:pa[u]=find(pa[u]);}
      |               ^
worst_reporter2.cpp:48:22: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   48 | void merg(int a,int b){
      |                      ^
worst_reporter2.cpp:53:17: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   53 | void dfs(int cur){
      |                 ^
worst_reporter2.cpp:73:28: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   73 | void solve(int cur,int root){
      |                            ^
worst_reporter2.cpp:97:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   97 | int32_t main(){
      |              ^
worst_reporter2.cpp: In function 'void setIO(std::string)':
worst_reporter2.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
worst_reporter2.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29276 KB Output is correct
2 Correct 6 ms 22104 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 7 ms 22196 KB Output is correct
5 Correct 9 ms 30552 KB Output is correct
6 Correct 12 ms 24156 KB Output is correct
7 Correct 10 ms 23900 KB Output is correct
8 Correct 10 ms 24412 KB Output is correct
9 Correct 14 ms 24160 KB Output is correct
10 Correct 9 ms 22796 KB Output is correct
11 Correct 8 ms 23644 KB Output is correct
12 Correct 9 ms 24412 KB Output is correct
13 Correct 8 ms 24412 KB Output is correct
14 Correct 13 ms 22876 KB Output is correct
15 Correct 11 ms 24096 KB Output is correct
16 Correct 11 ms 23900 KB Output is correct
17 Correct 7 ms 26200 KB Output is correct
18 Correct 6 ms 27736 KB Output is correct
19 Correct 10 ms 23128 KB Output is correct
20 Correct 9 ms 24172 KB Output is correct
21 Correct 11 ms 24156 KB Output is correct
22 Correct 9 ms 24012 KB Output is correct
23 Correct 6 ms 27736 KB Output is correct
24 Correct 12 ms 20348 KB Output is correct
25 Correct 13 ms 20316 KB Output is correct
26 Correct 7 ms 29528 KB Output is correct
27 Correct 9 ms 24152 KB Output is correct
28 Correct 9 ms 23388 KB Output is correct
29 Correct 8 ms 23388 KB Output is correct
30 Correct 11 ms 24408 KB Output is correct
31 Correct 9 ms 24412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29276 KB Output is correct
2 Correct 6 ms 22104 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 7 ms 22196 KB Output is correct
5 Correct 9 ms 30552 KB Output is correct
6 Correct 12 ms 24156 KB Output is correct
7 Correct 10 ms 23900 KB Output is correct
8 Correct 10 ms 24412 KB Output is correct
9 Correct 14 ms 24160 KB Output is correct
10 Correct 9 ms 22796 KB Output is correct
11 Correct 8 ms 23644 KB Output is correct
12 Correct 9 ms 24412 KB Output is correct
13 Correct 8 ms 24412 KB Output is correct
14 Correct 13 ms 22876 KB Output is correct
15 Correct 11 ms 24096 KB Output is correct
16 Correct 11 ms 23900 KB Output is correct
17 Correct 7 ms 26200 KB Output is correct
18 Correct 6 ms 27736 KB Output is correct
19 Correct 10 ms 23128 KB Output is correct
20 Correct 9 ms 24172 KB Output is correct
21 Correct 11 ms 24156 KB Output is correct
22 Correct 9 ms 24012 KB Output is correct
23 Correct 6 ms 27736 KB Output is correct
24 Correct 12 ms 20348 KB Output is correct
25 Correct 13 ms 20316 KB Output is correct
26 Correct 7 ms 29528 KB Output is correct
27 Correct 9 ms 24152 KB Output is correct
28 Correct 9 ms 23388 KB Output is correct
29 Correct 8 ms 23388 KB Output is correct
30 Correct 11 ms 24408 KB Output is correct
31 Correct 9 ms 24412 KB Output is correct
32 Correct 9 ms 32604 KB Output is correct
33 Correct 255 ms 96184 KB Output is correct
34 Correct 231 ms 71096 KB Output is correct
35 Correct 248 ms 93096 KB Output is correct
36 Correct 197 ms 71248 KB Output is correct
37 Correct 113 ms 52304 KB Output is correct
38 Correct 94 ms 47872 KB Output is correct
39 Correct 117 ms 75604 KB Output is correct
40 Correct 111 ms 75600 KB Output is correct
41 Correct 88 ms 75344 KB Output is correct
42 Correct 106 ms 59984 KB Output is correct
43 Correct 103 ms 60000 KB Output is correct
44 Correct 260 ms 120612 KB Output is correct
45 Correct 177 ms 84816 KB Output is correct
46 Correct 65 ms 47372 KB Output is correct
47 Correct 174 ms 69796 KB Output is correct
48 Correct 91 ms 63056 KB Output is correct
49 Correct 79 ms 63056 KB Output is correct
50 Correct 196 ms 64532 KB Output is correct
51 Correct 76 ms 52164 KB Output is correct
52 Correct 183 ms 70860 KB Output is correct
53 Correct 102 ms 63692 KB Output is correct
54 Correct 85 ms 75344 KB Output is correct
55 Correct 141 ms 73132 KB Output is correct
56 Correct 120 ms 79328 KB Output is correct
57 Correct 113 ms 82512 KB Output is correct
58 Correct 142 ms 78500 KB Output is correct
59 Correct 128 ms 78672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 29276 KB Output is correct
2 Correct 6 ms 22104 KB Output is correct
3 Correct 7 ms 22108 KB Output is correct
4 Correct 7 ms 22196 KB Output is correct
5 Correct 9 ms 30552 KB Output is correct
6 Correct 12 ms 24156 KB Output is correct
7 Correct 10 ms 23900 KB Output is correct
8 Correct 10 ms 24412 KB Output is correct
9 Correct 14 ms 24160 KB Output is correct
10 Correct 9 ms 22796 KB Output is correct
11 Correct 8 ms 23644 KB Output is correct
12 Correct 9 ms 24412 KB Output is correct
13 Correct 8 ms 24412 KB Output is correct
14 Correct 13 ms 22876 KB Output is correct
15 Correct 11 ms 24096 KB Output is correct
16 Correct 11 ms 23900 KB Output is correct
17 Correct 7 ms 26200 KB Output is correct
18 Correct 6 ms 27736 KB Output is correct
19 Correct 10 ms 23128 KB Output is correct
20 Correct 9 ms 24172 KB Output is correct
21 Correct 11 ms 24156 KB Output is correct
22 Correct 9 ms 24012 KB Output is correct
23 Correct 6 ms 27736 KB Output is correct
24 Correct 12 ms 20348 KB Output is correct
25 Correct 13 ms 20316 KB Output is correct
26 Correct 7 ms 29528 KB Output is correct
27 Correct 9 ms 24152 KB Output is correct
28 Correct 9 ms 23388 KB Output is correct
29 Correct 8 ms 23388 KB Output is correct
30 Correct 11 ms 24408 KB Output is correct
31 Correct 9 ms 24412 KB Output is correct
32 Correct 9 ms 32604 KB Output is correct
33 Correct 255 ms 96184 KB Output is correct
34 Correct 231 ms 71096 KB Output is correct
35 Correct 248 ms 93096 KB Output is correct
36 Correct 197 ms 71248 KB Output is correct
37 Correct 113 ms 52304 KB Output is correct
38 Correct 94 ms 47872 KB Output is correct
39 Correct 117 ms 75604 KB Output is correct
40 Correct 111 ms 75600 KB Output is correct
41 Correct 88 ms 75344 KB Output is correct
42 Correct 106 ms 59984 KB Output is correct
43 Correct 103 ms 60000 KB Output is correct
44 Correct 260 ms 120612 KB Output is correct
45 Correct 177 ms 84816 KB Output is correct
46 Correct 65 ms 47372 KB Output is correct
47 Correct 174 ms 69796 KB Output is correct
48 Correct 91 ms 63056 KB Output is correct
49 Correct 79 ms 63056 KB Output is correct
50 Correct 196 ms 64532 KB Output is correct
51 Correct 76 ms 52164 KB Output is correct
52 Correct 183 ms 70860 KB Output is correct
53 Correct 102 ms 63692 KB Output is correct
54 Correct 85 ms 75344 KB Output is correct
55 Correct 141 ms 73132 KB Output is correct
56 Correct 120 ms 79328 KB Output is correct
57 Correct 113 ms 82512 KB Output is correct
58 Correct 142 ms 78500 KB Output is correct
59 Correct 128 ms 78672 KB Output is correct
60 Correct 4 ms 29276 KB Output is correct
61 Correct 5 ms 31324 KB Output is correct
62 Correct 4 ms 31356 KB Output is correct
63 Correct 4 ms 29276 KB Output is correct
64 Correct 214 ms 80804 KB Output is correct
65 Correct 161 ms 64080 KB Output is correct
66 Correct 218 ms 81488 KB Output is correct
67 Correct 156 ms 63904 KB Output is correct
68 Correct 118 ms 50260 KB Output is correct
69 Correct 94 ms 46676 KB Output is correct
70 Correct 130 ms 55028 KB Output is correct
71 Correct 70 ms 43408 KB Output is correct
72 Correct 150 ms 64592 KB Output is correct
73 Correct 83 ms 54100 KB Output is correct
74 Correct 142 ms 64456 KB Output is correct
75 Correct 85 ms 52172 KB Output is correct
76 Correct 84 ms 51916 KB Output is correct
77 Correct 150 ms 64400 KB Output is correct
78 Correct 79 ms 52172 KB Output is correct
79 Correct 194 ms 80000 KB Output is correct
80 Correct 137 ms 63020 KB Output is correct
81 Correct 96 ms 52292 KB Output is correct
82 Correct 143 ms 64516 KB Output is correct
83 Correct 63 ms 37796 KB Output is correct
84 Correct 216 ms 60516 KB Output is correct
85 Correct 192 ms 60716 KB Output is correct
86 Correct 183 ms 57488 KB Output is correct
87 Correct 178 ms 61776 KB Output is correct
88 Correct 205 ms 64012 KB Output is correct