Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1051
Question:
Given the length and weight of some wood sticks, the installation time of the first wood stick is 1 minute. If the length and weight of the last wood stick do not exceed the length and weight of the next wood stick, therefore, the installation time is not required. Otherwise, it will take 1 minute.
Find the minimum installation time.
For example: (), and)
It takes only 2 minutes. (The first root takes 1 minute, and then () It takes 1 minute to go)
Ideas:
I tried DP. I knew it was greedy at first glance. I don't know who is using DP after half a day. Qaq
If you are not talking about it, find it back when selecting a selection until you cannot find the uninstalled wood stick.
Complexity O (N ^ 2)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 5000;struct wooden{int L, W;bool operator <(const wooden& x)const{if (L == x.L)return W < x.W;return L < x.L;}}a[MAXN];int main(){int T;scanf("%d", &T);while (T--){int n;scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d%d", &a[i].L, &a[i].W);sort(a, a + n);bool used[MAXN] = { 0 };int ans = 1;for (int i = 0; i < n; i++){if (used[i])continue;used[i] = true;int L=a[i].L, W=a[i].W;for (int j = i + 1; j < n; j++){if (used[j])continue;if (a[j].W >= W && a[j].L >= L){used[j] = true;L = a[j].L;W = a[j].W;}}bool find = false;for (int j = i + 1; j < n; j++){if (!used[j]){find = true;break;}}if (!find)break;ans++;}printf("%d\n", ans);}return 0;}
HDU 1051 wooden sticks greedy