Submission #1266640

#TimeUsernameProblemLanguageResultExecution timeMemory
1266640syanvuGroup Photo (JOI21_ho_t3)C++20
0 / 100
0 ms324 KiB
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define SS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr); #pragma optimize("g", on) #pragma GCC optimize("03") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native") #define int long long #define all(x) x.begin(),x.end() #define F first #define S second using namespace std; // using namespace __gnu_pbds; // #define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> const int LG = 21,N = 2e5+1,inf = 1e18,MOD = 998244353; const double eps = 1e-9; int T; int f[N]; int n; void add(int i,int x){ while(i<=n){ f[i]+=x; i+=i&-i; } } int get(int i){ int res=0; while(i){ res+=f[i]; i-=i&-i; } return res; } int range(int l,int r){ return get(r)-get(l); } void solve() { cin>>n; int h[n+1]; int id[n+1]; for(int i=1;i<=n;i++){ cin>>h[i]; id[h[i]]=i; } int cost[n+2][n+2]={},pref[n+2][n+2]={}; for(int r=1;r<=n;r++){ for(int i=1;i<=r;i++) pref[r][id[i]]++; for(int i=1;i<=n;i++){ pref[r][i]+=pref[r][i-1]; } for(int l=r;l>=1;l--){ cost[l][r]=cost[l+1][r]+(pref[r][n]-pref[r][id[l]])-get(id[l]); if(r<id[l]) cost[l][r]+=id[l]-r; add(id[l],1); } for(int i=1;i<=n;i++) f[i]=0; } vector<int> dp(n+1,inf); dp[0]=0; for(int r=1;r<=n;r++){ for(int l=r;l>=1;l--){ dp[r]=min(dp[r],dp[l-1]+cost[l][r]); } } cout<<dp[n]; return; } signed main() { // freopen("deleg.in","r",stdin); // freopen("deleg.out","w",stdout); SS int t = 1; if (T) { cin >> t; } while (t--) { solve(); } } /* 1,2,3,...,n-2,n-1,n good array is reverse of subsequence of subarray because the condition a[i]-a[i+1]<=1 must hold PROOF { Suppose on the position i there is number x the next number which we can pick is x-1 or y>x when we take number which is greater than x, x-1 number must be taken on prefix Otherwise y>x will only decrease by 1 and the minimum number on suffix will x+1, and x+1-x-1=2 condition doesnt hold } dp[i][j] -> prefix i, last number on reversed array 1<=j<=i dp[i] = dp[j-1] + op[j] */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...