QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334542#8050. Random Permutationucup-team1055WA 827ms4736kbC++143.5kb2024-02-22 04:14:412024-02-22 04:14:41

Judging History

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

  • [2024-02-22 04:14:41]
  • 评测
  • 测评结果:WA
  • 用时:827ms
  • 内存:4736kb
  • [2024-02-22 04:14:41]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <typename T> bool chmin(T &a, const T &b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <typename T> bool chmax(T &a, const T &b) {
    if (a >= b) return false;
    a = b;
    return true;
}

using namespace std;

vector<int> random_permutation(int n){
	random_device seed_gen;
	mt19937 engine(seed_gen());
	vector<int> p(n);
	iota(p.begin(), p.end(), 1);
	shuffle(p.begin(), p.end(), engine);
	return p;
}

ll naive(int n, vector<int> p){
	ll ans = 0;
	rep(i,0,n){
		priority_queue<int, vector<int>, greater<int>> larger;
		priority_queue<int> smaller;
		rep(j,i,n){
			if (smaller.empty()){
				smaller.push(p[j]);
			}else if(larger.empty()){
				smaller.push(p[j]);
			}else if(larger.top() > p[j]){
				smaller.push(p[j]);
			}else{
				larger.push(p[j]);
			}
			while ((int)smaller.size() < (int)larger.size()){
				smaller.push(larger.top());
				larger.pop();
			}
			while ((int)smaller.size() > (int)larger.size() + 1){
				larger.push(smaller.top());
				smaller.pop();
			}
			ans += smaller.top();
		}
	}
	return ans;
}

const int mx = 1000;
const int mx_sub1 = 500;
const int mx_sub2 = 300;

ll fast(int n, vector<int> p){
	ll ans = 0;
	vector<int> left(2 * mx + 1);
	vector<int> right(2 * mx + 1);
	rep(i,0,n){
		fill(left.begin(), left.end(), 0);
		fill(right.begin(), right.end(), 0);
		int targ = p[i];
		{
			int cnt = mx;
			left[cnt]++;
			rrep(j,0,i){
				if (targ < p[j]) cnt--;
				else cnt++;
				if (abs(cnt - mx) > mx) break;
				left[cnt]++;
			}
		}
		{
			int cnt = mx + 1;
			right[cnt]++;
			rep(j,i+1,n){
				if (targ < p[j]) cnt--;
				else cnt++;
				if (abs(cnt - mx) > mx) break;
				right[cnt]++;
			}
		}
		ll var = 0;
		rep(j, 0, 2 * mx){
			var += (ll)right[2 * mx - j] * (left[j] + left[j + 1]);
		}
		ans += var * targ;
	}
	return ans;
}


ll fast2(int n, vector<int> p){
	ll ans = 0;
	vector<int> left(2 * mx + 1);
	vector<int> right(2 * mx + 1);
	vector<int> a(n, +1);
	
	vector<int> q(n);
	rep(i,0,n){
		q[p[i] - 1] = i;
	}
	reverse(q.begin(), q.end());

	for (int i: q){

		int targ = mx;
		if (min(n + 1 - p[i], p[i]) < n / 8){
			targ = mx_sub2;
		}else if (min(n + 1 - p[i], p[i]) < n / 4){
			targ = mx_sub1;
		}

		fill(left.begin() + mx - targ, left.begin() + mx + targ, 0);
		fill(right.begin() + mx - targ, right.begin() + mx + targ, 0);

		{
			int cnt = mx;
			left[cnt]++;
			rrep(j,0,i){
				cnt += a[j];
				if (abs(cnt - mx) > targ) break;
				left[cnt]++;
			}
		}

		{
			int cnt = mx + 1;
			right[cnt]++;
			rep(j,i+1,n){
				cnt += a[j];
				if (abs(cnt - mx) > targ) break;
				right[cnt]++;
			}
		}

		ll var = 0;
		rep(j, mx - targ, mx + targ - 1){
			var += (ll)right[2 * mx - j] * (left[j] + left[j + 1]);
		}
		ans += var * p[i];
		a[i] = -1;
	}

	return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	//*
	int n; cin >> n;
	vector<int> p(n);
	rep(i,0,n){
		cin >> p[i];
	}
	//*/

	/*
	int n = 300000;
	vector<int> p = random_permutation(n);
	//*/
	
	//cout << naive(n, p) << endl;
	cout << fast2(n, p) << endl;
	//cout << fast(n, p) << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: -100
Wrong Answer
time: 827ms
memory: 4736kb

input:

100000
56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...

output:

378235589483959

result:

wrong answer 1st numbers differ - expected: '250202478701074', found: '378235589483959'