HDU 1051(贪心)

超时代码“

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;

const int INF = 0x3f3f3f3f;
struct node
{
    int x;
    int y;
}stick[5005];
bool f[5005];
bool cmp(node a, node b)
{
    if (a.x == b.x)
        return a.y < b.y;
    else
        return a.x < b.x;
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        node temp[5005];
        memset(f, true, sizeof(f));
        int i, j;
        int n;
        cin >> n;
        for (i = 1; i <= n; i++)
        {
            scanf("%d%d",&stick[i].x ,&stick[i].y);
        }
        sort(stick + 1, stick + n + 1, cmp);
        int count = 0;
        int Sum = 0;
        int temp1 = n;
        while (count != n)
        {
            int k = 1;
            int num = 0;
            for (i = 1; i <= temp1; i++)
            {
                if (f[i] == true)
                {
                    temp[k++].y = stick[i].y;
                    num++;
                }
            }
            for (i = 1; i <= num; i++)
            {
                if (temp[i].y >=temp[i - 1].y)
                {
                    f[i] = false;
                    count++;            
                }
            }
            Sum++;
            temp1 = num;
        }
        cout << Sum << endl;
    }
    return 0;
}

正确代码


#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;

const int INF = 0x3f3f3f3f;
struct node
{
    int x;
    int y;
    int flag;
}stick[5005];

bool cmp(node a, node b)
{
    if (a.x == b.x)
        return a.y < b.y;
    else
        return a.x < b.x;
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        memset(stick, 0, sizeof(stick));
        int i, j;
        int n;
        cin >> n;
        for (i = 1; i <= n; i++)
        {
            scanf("%d%d",&stick[i].x ,&stick[i].y);
        }
        sort(stick + 1, stick + n + 1, cmp);
        int count = 0;
        int Sum = 0;
        for (i = 1; i<=n; i++)
        {
            if (stick[i].flag == 0)
            {
                int temp;
                count++;
                temp = i;
                for (j = i + 1; j<=n; j++)
                {
                    if (stick[j].flag == 0 && stick[temp].y <= stick[j].y)
                    {
                        temp = j;
                        stick[j].flag = 1;
                    }
                }
            }
        }
        printf("%d\n", count);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值