HDUOJ 1051 Wooden Sticks

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define CLOSE() ios::sync_with_stdio(false)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
const int maxn = 1e5 + 5;
using LL = long long;
using UI = unsigned int;
using namespace std;
//------------------------------------------------------------------------------------------//
/*
贪心算法,每步选取最优,在这题上就是每一次选出一个无消耗t的木头,直到全部选完。
为什么要这样先排序?为了选出所有单调子序列。。好吧,我还没有完全理解这题的贪心算法。
*/
struct A {
	int x, y;
	bool operator<(const A &rhs) {
		if (x != rhs.x) return x < rhs.x;
		else return y < rhs.y;
	}//定义比较运算时不能加入等号
}a[5005];
int vis[5005] = { 0 };

int main() {
	int t;
	//IN(); OUT();
	scanf("%d", &t);
	while (t--) {
		CLEAR(vis, 0);
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
		sort(a, a+n);
		//for(int i = 0; i < n; ++i) printf("(%d, %d) ", a[i].x, a[i].y); cout << endl;
		int cnt = 0, num = 0, now = 0;
		while (cnt != n) {
			int flag = 1;
			for (int i = 0; i < n; i++) {
				if (flag && !vis[i]) flag = 0, now = i, vis[i] = 1, cnt++;
				else if (!vis[i] && a[i].y >= a[now].y) {
					now = i;
					vis[i] = 1;
					cnt++;
				}
			}
			num++;
		}
		cout << num << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值