QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306811 | #7993. 哈密顿 | analysis | WA | 55ms | 4828kb | C++14 | 1.6kb | 2024-01-17 11:50:47 | 2024-01-17 11:50:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct st{
int id;
int v;
st(int id,int v):id(id),v(v){}
};
bool operator<(st a,st b)
{
if(a.v != b.v)return a.v < b.v;
else return a.id < b.id;
}
bool vis[100005];
priority_queue<st> s1,s2;
int n,ans;
int lnum;
bool func(bool must=false)
{
while(!s1.empty() && vis[s1.top().id])s1.pop();
while(!s2.empty() && vis[s2.top().id])s2.pop();
if(s1.empty() || s2.empty())return false;
st a = s1.top(),b = s2.top();
s1.pop();
s2.pop();
if(a.id != b.id)
{
if(must || a.v + b.v > 0)return ans += a.v + b.v,1;
else return false;
}
else
{
if(!must)return false;
else
{
while(!s1.empty() && vis[s1.top().id])s1.pop();
while(!s2.empty() && vis[s2.top().id])s2.pop();
if(s1.empty() && s2.empty())return false;
else if(s1.empty())
{
st d = s2.top();
s2.pop();
ans += a.v + d.v;
return true;
}
else if(s2.empty())
{
st c = s1.top();
s1.pop();
ans += b.v + c.v;
return true;
}
else
{
st c = s1.top();
st d = s2.top();
if(a.v + d.v > b.v + c.v)
{
s2.pop();
ans += a.v + d.v;
}
else
{
s1.pop();
ans += b.v + c.v;
}
return true;
}
}
}
}
int ts1,ts2;
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
ts1 += a-b;
ts2 += b-a;
if(a >= b)lnum++;
ans += abs(a - b);
int v1 = 2 * min(a,b);
int v2 = 2 * max(a,b);
v2 = -v2;
s1.push(st(i,v1));
s2.push(st(i,v2));
}
if(lnum != n && lnum != 0)
{
func(true);
}
while(func());
cout<<max(ans,max(ts1,ts2));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3456kb
input:
2 732734236 669531729 368612323 916696032
output:
484881202
result:
ok single line: '484881202'
Test #3:
score: -100
Wrong Answer
time: 55ms
memory: 4828kb
input:
96739 960257511 790393830 766983782 374475134 476482176 529858300 310917487 76906076 81191659 653180897 119832756 362638666 228783015 626981197 74837554 781854003 663345706 153150021 576244413 708232220 842559077 417230462 249522463 815253339 391663517 742521049 60507117 258429449 112143256 81408991...
output:
1951517437
result:
wrong answer 1st lines differ - expected: '48459768018811', found: '1951517437'