Submission #1080818

#TimeUsernameProblemLanguageResultExecution timeMemory
10808188pete8Worst Reporter 4 (JOI21_worst_reporter4)C++17
100 / 100
260 ms120612 KiB
#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 (stderr)

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);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...