QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546507 | #5507. Investors | Fika# | WA | 3ms | 3908kb | C++14 | 1.7kb | 2024-09-04 06:45:31 | 2024-09-04 06:45:31 |
Judging History
answer
#include<bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
using namespace std;
#define rep(i,a,b) for(ll i = a; i<b;i++)
#define rrep(i,a,b) for(ll i = b-1; i>=a;i--)
#define trav(x,a) for(auto &x: a)
#define all(v) v.begin(),v.end()
#define sz(v) ll(v.size())
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
struct FT {
vector<ll> s;
FT(int n) : s(n) {}
void update(int pos, ll dif) { // a[pos] += dif
for (; pos < sz(s); pos |= pos + 1) s[pos] += dif;
}
ll query(int pos) { // sum of values in [0, pos)
ll res = 0;
for (; pos > 0; pos &= pos - 1) res += s[pos-1];
return res;
}
};
void solve(){
ll n,k;
cin>>n>>k;
vl a(n);
rep(i,0,n) cin>>a[i];
vl as = a;
sort(all(as));
map<ll,ll> comp;
rep(i,0,n) comp[as[i]] = i;
rep(i,0,n) a[i] = comp[a[i]];
reverse(all(a));
vector<vl> invs(n,vl(n+1));
rep(i,0,n){
FT ft(n);
rep(j,i+1,n+1){
invs[i][j] = invs[i][j-1] + ft.query(a[j-1]);
ft.update(a[j-1],1);
// cout<<"invs["<<i<<"]["<<j<<"]="<<invs[i][j]<<endl;
}
}
vector<vl> dp(k+2,vl(n+1,1e18));
dp[0][n] = 0;
rep(ki,1,k+2){
ll optNext = n;
for(ll i = n-1;i>=0;i--){
auto f = [&](ll j){
return dp[ki-1][j]+invs[i][j];
};
while(optNext>i+1 && f(optNext-1)<=f(optNext)) optNext--;
dp[ki][i] = f(optNext);
// cout<<"dp["<<ki<<"]["<<i<<"]="<<dp[ki][i]<<", optNext="<<optNext<<endl;
}
}
cout<<dp[k+1][0]<<"\n";
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll t; cin>>t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
input:
2 6 1 4 5 6 2 2 1 6 2 4 5 6 2 2 1
output:
2 0
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3908kb
input:
349 6 2 2 1 2 1 2 1 48 12 42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21 48 12 1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...
output:
1 98 108 216 0 10 1 0 79 81 89 0 0 0 1 0 0 0 0 0 0 0 0 322 3 0 0 1 0 1000000000000000000 0 0 0 0 1 1000000000000000000 3 0 0 1 0 0 2 0 0 1 6 0 0 0 0 0 0 0 0 2 0 2 0 1000000000000000000 71 328 0 0 153 4 0 1 0 0 3 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 2 0 0 0 0 0 0 0 20 1 30 0 0 0 0 1 156 ...
result:
wrong answer 2nd lines differ - expected: '18', found: '98'