P1090 P1323 数据结构 优先队列

P1090 合并果子

一道模板题

#include <queue>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
priority_queue <int,vector<int>,greater<int> > q;
int main( ){
    int n;
    scanf("%d",&n);
    int u[n+5];
    for (int i=1;i<=n;i++){
        scanf("%d",&u[i]);
        q.push(u[i]);
    }
    int k=0;
    for(int i=1;i<=n-1;i++){
        int y=q.top();
        q.pop();
        int z=q.top();
        q.pop();
        k+=y+z;
        q.push(y+z);
    }
    printf("%d",k);
    return 0;
}

P1323 删数问题

LGTJ

使用BFS?
~~~

include

include

include

using namespace std;
priority_queue<int,vector,greater > q;
int a[30001],g=0;
int k,m;
int st[200001],l=0,sum=0;
void bfs()
{
q.push(1);
while(!q.empty())
{
int p=q.top(),d=0;
q.pop();
q.push(2p+1);
q.push(4
p+5);
a[++g]=p;
while(p)
{
d=d*10+p%10;
p/=10;
}
while(d)
{
if(l==0)
st[++l]=d%10;
else
{
while(l!=0&&d%10>st[l]&&sum<m)
{
l--;
sum++;
}
st[++l]=d%10;
}
d/=10;
}
if(g==k)
return;
}
}
int main()
{
cin>>k>>m;
bfs();
int i;
for(i=1;i<=g;i++)
cout<<a[i];
cout<<endl;
for(i=1;i<=l-(m-sum);i++)
cout<<st[i];
return 0;
}
~~~

12-20 23:52