链接
https://vjudge.net/problem/UVA-11136
题解
优先队列
代码
#include <bits/stdc++.h>
#define maxn 1000010
#define cl(x) memset(x,0,sizeof(x))
#define mkp(x,y) make_pair((x),(y))
using namespace std;
typedef long long ll;
priority_queue< pair<ll,ll> > big;
priority_queue< pair<ll,ll> , vector< pair<ll,ll> > , greater< pair<ll,ll> > > small;
ll read(ll x=0)
{
ll c, f=1;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-48;
return f*x;
}
bool mark[maxn];
int main()
{
ll N, K, x, tot, ans;
while(N=read())
{
tot=0;
ans=0;
cl(mark);
while(!big.empty())big.pop();
while(!small.empty())small.pop();
for(auto i=1;i<=N;i++)
{
K=read();
for(auto j=1;j<=K;j++)
{
x=read();
tot++;
big.emplace( mkp(x,tot) );
small.emplace( mkp(x,tot) );
}
auto mx = big.top(); big.pop();
while(mark[mx.second])
{
mx = big.top(); big.pop();
}
auto mn = small.top(); small.pop();
while(mark[mn.second])
{
mn = small.top(); small.pop();
}
mark[mx.second] = mark[mn.second] = true;
ans += mx.first-mn.first;
}
printf("%lld\n",ans);
}
return 0;
}
本文详细解析了Vjudge网题UVA-11136的解决方案,通过使用优先队列进行数据处理,实现了高效的数据结构操作。文章深入探讨了优先队列在解决特定问题时的应用技巧,并提供了完整的C++代码实现。
965

被折叠的 条评论
为什么被折叠?



