QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619755#7689. Flipping Cardsty09#WA 665ms40956kbC++172.3kb2024-10-07 15:17:212024-10-07 15:17:21

Judging History

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

  • [2024-10-07 15:17:21]
  • 评测
  • 测评结果:WA
  • 用时:665ms
  • 内存:40956kb
  • [2024-10-07 15:17:21]
  • 提交

answer

// #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(nullptr),cout.tie(nullptr)
#define inf 0x3f3f3f3f
#define ll long long
#define endl '\n'
#define T() int ____t;cin>>____t;while(____t--)
using namespace std;
constexpr int N=2e6+10,mod=1e9+7;
#define int ll
template <typename T> // 选用int || long long
struct Mid_set
{
	multiset<T, greater<T>> st1; //小于中位数的数的集合
	multiset<T> st2; //大于等于中位数的集合 
	//   st2.size() ≥ st1.size();
	ll sum1 = 0, sum2 = 0;
	void split()
	{
		while (st1.size() < st2.size())
		{
			T now = *st2.begin();
			sum2 -= now;
			sum1 += now;
			st1.insert(now);
			st2.erase(st2.begin());
		}
		while (st1.size() > st2.size())
		{
			T now = *st1.begin();
			st2.insert(now);
			sum1 -= now;
			sum2 += now;
			st1.erase(st1.begin());
		}
	}
	void insert(T x)
	{
		st2.insert(x);
		sum2 += x;
		split();
	}
	void erase(T x)
	{
		if (st1.count(x))
		{
			st1.erase(st1.lower_bound(x));
			sum1 -= x;
		}
		else
		{
			st2.erase(st2.lower_bound(x));
			sum2 -= x;
		}
		split();
	}
	bool empty()
	{
		return st2.empty();
	}
	T query()//返回中位数
	{
		return *st2.begin();//有奇数个数
	}
	ll become_mid() // 通过±1,将所有数变相同所需最少操作数
	{
		T mid = query();
		int s1 = st1.size(), s2 = st2.size();
		ll ans = 1ll * s1 * mid - sum1 + sum2 - 1ll * s2 * mid;
		return ans;
	}
	void clear(){
		st1.clear();
		st2.clear();
	}
};
void solve(){
	int n;
	cin>>n;
	vector<int>a(n+1),b(n+1);
	Mid_set<int>p;
	Mid_set<int>lra;
	Mid_set<int>lrb;
	for(int i=1;i<=n;i++){
		cin>>a[i]>>b[i];
		p.insert(a[i]);
	}
	int med=p.query();
	int l=1,r=1;
	int ans=med;
	for(int i=1;i<=n;i++){
		p.insert(b[i]);
		p.erase(a[i]);
		lra.insert(a[i]);
		lrb.insert(b[i]);
		int now=p.query();
		ans=max(ans,now);
		r=i;
		if(now<med){
			for(int j=l;j<=r;j++){
				p.insert(a[j]);
				p.erase(b[j]);
			}
			l=i+1;
		}
		else if(now==med){
			if(lra.query()>lrb.query()){
				for(int j=l;j<=r;j++){
					p.insert(a[j]);
					p.erase(b[j]);
				}
				l=i+1;
				lra.clear();
				lrb.clear();
			}
		}
	}
	cout<<ans<<endl;
}
signed main()
{
	ios;
//	T()
	solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
3 6
5 2
4 7
6 4
2 8

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

1
2 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

1
212055293 384338286

output:

384338286

result:

ok 1 number(s): "384338286"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3852kb

input:

99
749159996 323524232
125448341 365892333
481980673 143665393
394405973 44741918
687549448 513811513
287088118 385131171
11865696 666468353
449920567 373650719
671547289 116780561
41003675 671513243
351534153 507850962
374160874 985661954
222519431 600582098
987220654 704142246
856147059 37783620
1...

output:

528957505

result:

ok 1 number(s): "528957505"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3852kb

input:

101
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000...

output:

1000000000

result:

ok 1 number(s): "1000000000"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

103
66 46
41 70
52 76
26 5
54 2
78 21
22 39
100 15
73 94
56 7
45 72
76 80
6 67
12 8
86 53
26 1
5 57
90 44
81 85
2 70
32 79
95 42
97 37
87 93
2 21
21 42
29 25
61 35
98 99
33 46
51 10
45 56
40 75
71 25
79 37
75 10
34 98
1 22
40 12
14 81
83 29
51 12
37 96
74 11
30 49
39 34
68 68
36 17
3 55
41 32
22 92
...

output:

55

result:

ok 1 number(s): "55"

Test #7:

score: 0
Accepted
time: 7ms
memory: 4640kb

input:

5555
884376710 45124731
564350738 110566376
82266416 71890085
742302826 424812817
441684523 786251012
1208704 118200627
206028578 736388312
179371956 412238226
562783304 721943945
855108903 710808533
969831121 89689888
833625410 9559177
39704951 153974475
778740527 562223006
103796470 968790365
2050...

output:

520583648

result:

ok 1 number(s): "520583648"

Test #8:

score: 0
Accepted
time: 102ms
memory: 9308kb

input:

55555
407954959 925854335
331922620 685714089
683072900 978458276
462828931 975317170
524480939 832278948
759453127 157033854
246638012 738429531
423955730 483191182
541683890 709827850
309667569 360334083
797868492 960421332
981833589 59185699
53482766 56438082
56804787 566838744
76359614 376208064...

output:

506121745

result:

ok 1 number(s): "506121745"

Test #9:

score: 0
Accepted
time: 358ms
memory: 24180kb

input:

155555
598245381 667586986
31130797 648468145
839307239 705727216
146732291 106428416
157005415 48720198
868350611 519788697
499861343 881904424
995572615 441419419
119284329 335071863
413173260 485799366
519666020 413812470
731682515 630429185
743423219 948725454
882249618 79486422
811743485 533827...

output:

500787268

result:

ok 1 number(s): "500787268"

Test #10:

score: -100
Wrong Answer
time: 665ms
memory: 40956kb

input:

255555
819179537 949891169
705391237 410027888
777107329 25520945
513926806 183876333
290510034 908000460
36748629 813828746
497181988 502052202
215712828 958261098
141598186 205382071
126267481 211448419
398156140 601079636
499201742 99939455
587732806 610668886
293051727 877113727
82610919 6580579...

output:

502455386

result:

wrong answer 1st numbers differ - expected: '502458020', found: '502455386'