QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306808 | #7993. 哈密顿 | analysis | WA | 0ms | 3584kb | C++14 | 1.6kb | 2024-01-17 11:42:53 | 2024-01-17 11:42:54 |
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;
}
}
}
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
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<<ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
3 1 10 8 2 4 5
output:
10
result:
ok single line: '10'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3572kb
input:
2 732734236 669531729 368612323 916696032
output:
116957610
result:
wrong answer 1st lines differ - expected: '484881202', found: '116957610'