数据结构第一讲

题目

 优先队列POJ - 2431 

1.

Expedition

 POJ - 2431 

//#include <bits/stdc++.h>
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iomanip>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = 1e4 + 5;
const double eps = 1e-6;
int n,l,p;
P a[maxn];
int main()
{
    scanf("%d",&n);
    for(int i = 0;i < n;i++)
    {
        scanf("%d%d",&a[i].first,&a[i].second);
    }
    scanf("%d%d",&l,&p);
    for(int i = 0;i < n;i++) a[i].first = l - a[i].first;
    sort(a,a + n);
    a[n].first = l,a[n].second = 0,n++;
    priority_queue<int> que;
    //ans:加油次数,pos:现在所在位置,tank:邮箱中汽油的量
    int ans = 0,pos = 0,tank = p;
    for(int i = 0;i < n;i++)
    {
        int d = a[i].first - pos;
        while(tank < d)
        {
            if(que.empty())
            {
                printf("-1");
                return 0;
            }
            tank += que.top();
            que.pop();
            ans++;
        }
        tank -= d;
        pos = a[i].first;
        que.push(a[i].second);
    }
    printf("%d\n",ans);
    return 0;
}

2.优先队列k叉哈夫曼树 HDU - 5884

Sort

 HDU - 5884

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int n,m;
int solve(int k,priority_queue<int,vector<int>,greater<int> > q)
{
    int ans = 0,x;
    if((n-1) % (k-1)!=0)
    {
        int num = ((n - 1) % (k - 1)) + 1;
        while(num--)
        {
            x = q.top();
            q.pop();
            ans += x;
        }
    }
    if(ans)
        q.push(ans);
    if(ans > m) return -1;
    while(q.size() > 1)
    {
        int y = 0;
        for(int i=1; i<=k; i++)
        {
            if(q.empty()) break;
            x = q.top();
            q.pop();
            y += x;
        }
        q.push(y);
        ans += y;
        if(ans > m) return -1;
    }
    return ans;
}
int main()
{
    int t,x;
    priority_queue<int,vector<int>,greater<int> > q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        while(!q.empty()) q.pop();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&x);
            q.push(x);
        }
        int l = 2;
        int r = n;
        while(l < r)
        {
            int m = l + (r - l) / 2;
            int ans = solve(m,q);
            if(ans == -1) l = m + 1;
            else r = m;
        }
        printf("%d\n",l);
    }
    return 0;
}

3.优先队列 UVALive - 3135

4.并查集 POJ - 2524 

Ubiquitous Religions

 POJ - 2524 

//#include <bits/stdc++.h>
#include <cstdio>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
//typedef pair<int,int> P;
const int maxn = 50000 + 2;
#define mod(x) (x % MOD)
#define FRE() freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
int n,m,fa[maxn];
void init()
{
    for(int i = 1;i <= n;i++) fa[i] = i;
}
int found(int x)
{
    if(fa[x] == x) return x;
    return fa[x] = found(fa[x]);
}
int main()
{
    int kase = 1;
    while(scanf("%d%d",&n,&m) && n && m)
    {
        init();
        int x,y;
        for(int i = 0;i < m;i++)
        {
            scanf("%d%d",&x,&y);
            int fx = found(x);
            int fy = found(y);
            if(fx != fy)
            {
                n--;
                fa[fx] = fy;
            }
        }
        printf("Case %d: %d\n",kase++,n);
    }
    return 0;
}

5.并查集UVA - 1160

6.并查集POJ - 1182 

7.二叉搜索树HDU - 5444

//#include <bits/stdc++.h>
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iomanip>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = 1e4 + 5;
const double eps = 1e-6;
int t,n;
struct node
{
    int val;
    node *lch,*rch;
};
node *inser(node *p,int x)
{
    if(p == NULL)
    {
        node *q = new node;
        q->val = x;
        q->lch = q->rch = NULL;
        return q;
    }
    else
    {
        if(x < p->val) p->lch = inser(p->lch,x);
        else p->rch = inser(p->rch,x);
        return p;
    }
}
bool found(node *p,int x)
{
    if(p == NULL) return false;
    else if(x == p->val) return true;
    else if(x < p->val)
    {
        printf("E");
        return found(p->lch,x);
    }
    else
    {
        printf("W");
        return found(p->rch,x);
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int x,q; node *p = NULL;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&x);
            p = inser(p,x);
        }
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d",&x);
            found(p,x);
            printf("\n");
        }
    }
    return 0;
}

8.二叉搜索树HDU - 3791

9.最近公共祖先LCAUVA - 11354 

10.最近公共祖先LCAHDU - 5452

发布了133 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sgsyacm/article/details/90597694
今日推荐