QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#853336#5946. Up and DownJEdward18 ✓9ms4084kbC++202.3kb2025-01-11 16:38:172025-01-11 16:38:17

Judging History

你现在查看的是最新测评结果

  • [2025-01-11 16:38:17]
  • 评测
  • 测评结果:18
  • 用时:9ms
  • 内存:4084kb
  • [2025-01-11 16:38:17]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x & -x)
#define all(s) s.begin(), s.end()
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define here system("pause")
using namespace std;
template <class T>
inline void read(T &x){
	x = 0;
	char c = getchar();
	bool f = 0;
	for (; !isdigit(c); c = getchar()) f ^= (c == '-');
	for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	x = f ? -x : x;
}
template <class T>
inline void write(T x){
	if (x < 0) putchar('-'), x = -x;
	if (x < 10) putchar(x + 48);
	else write(x / 10), putchar(x % 10 + 48);
}
template<typename T> void chkmin(T& lhs, const T& rhs) { lhs = lhs > rhs ? rhs : lhs; }
template<typename T> void chkmax(T& lhs, const T& rhs) { lhs = lhs < rhs ? rhs : lhs; }

const int N = 1e4 + 5;
const int mod = 998244353;
const int INF = 8e18 + 9;
const double eps = 1e-9;
const double Pi = acos(-1);
template <typename T>
struct fenwick{
	int n;
	vector<T> tr;
	
	fenwick (int n_ = 0){
		init(n_);
	}
	
	void init(int n_){
		n = n_;
		tr.assign(n, T{});
	}
		
	void add(int x, T d){
		for(int i=x;i<N;i+=lowbit(i)){
			tr[i] += d;
		}
	}
	
	int gsum(int x){
		T res = 0;
		for(int i=x;i;i-=lowbit(i)){
			res = res + tr[i];
		}
		return res;
	}
};
inline void sol(){
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        b[i] = a[i];
    }
    fenwick<int> tr(N), fr(N);
    vector<int> f(n), g(n);
    sort(all(b));
    for(int i=0;i<n;i++){
        a[i] = lower_bound(all(b), a[i]) - b.begin() + 1;
        a[i] = n + 4 - a[i];
        f[i] = tr.gsum(a[i]);
        tr.add(a[i], 1);
    }
    for(int i=n-1;i>=0;i--){
        //cout << a[i] << " \n"[i == 0];
        g[i] = fr.gsum(a[i]);
        fr.add(a[i], 1);
    }

    int ans = 0;
    for(int i=0;i<n;i++){
        //cout << " " << f[i] << " " << g[i] << " \n"[i == n - 1];
        ans += min(f[i], g[i]);
    }
    cout << ans << endl;
}
signed main(){
	IOS;
	int tc = 1;
	cin >> tc;
	cout << fixed << setprecision(10);
	for(int i=1;i<=tc;i++){
		cout << "Case #" << i << ": ";
		sol();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 7
Accepted

Test #1:

score: 7
Accepted
time: 1ms
memory: 4084kb

input:

100
8
5442 347 1402 5373 9469 2281 4003 219
9
4957 3355 337 693 1627 1372 2093 4151 9135
7
127 194 735 647 584 399 288
10
531 510 496 247 70 99 398 502 515 549
10
841 629 412 310 83 145 334 611 750 947
10
244 7064 6465 7404 5190 9149 6902 211 1861 7634
10
3341 8102 4954 5745 8455 379 2986 6904 494 8...

output:

Case #1: 4
Case #2: 13
Case #3: 0
Case #4: 20
Case #5: 20
Case #6: 8
Case #7: 10
Case #8: 0
Case #9: 3
Case #10: 3
Case #11: 10
Case #12: 3
Case #13: 13
Case #14: 12
Case #15: 7
Case #16: 20
Case #17: 5
Case #18: 0
Case #19: 9
Case #20: 2
Case #21: 12
Case #22: 7
Case #23: 13
Case #24: 20
Case #25: ...

result:

ok 100 lines

Subtask #2:

score: 11
Accepted

Test #2:

score: 11
Accepted
time: 9ms
memory: 3892kb

input:

100
615
148938 436018 160290 275822 869105 524968 565096 173649 623300 601127 564266 402562 534135 756541 87045 587390 497223 525981 14418 854242 376133 6737 747544 749040 939763 661848 541580 584761 578805 691578 421947 756284 597479 485235 874891 872708 188420 952787 244407 808782 156986 343427 16...

output:

Case #1: 47639
Case #2: 0
Case #3: 0
Case #4: 49936
Case #5: 41538
Case #6: 1024
Case #7: 122641
Case #8: 96003
Case #9: 25614
Case #10: 72121
Case #11: 135655
Case #12: 1657
Case #13: 0
Case #14: 22582
Case #15: 64
Case #16: 240
Case #17: 70559
Case #18: 75514
Case #19: 44058
Case #20: 22558
Case #...

result:

ok 100 lines

Extra Test:

score: 0
Extra Test Passed