HDUOJ 1051 Wooden Sticks

xiaoxiao2021-02-28  133

#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; }
转载请注明原文地址: https://www.6miu.com/read-37872.html

最新回复(0)