题意
若木棍长度和权值是升序( l <= l’ and w <= w’),那么这一个序列的安装时间是一分钟。问如何排序木棍使安装时间最短
题解
将木棍长度排序,然后找权值的升序序列。
代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 100006;
int dp[MAXN];
struct sticks
{
int len;
int wei;
};
sticks st[MAXN];
bool vis[MAXN];
int T,n;
int result;
struct comp
{
bool operator()(sticks a,sticks b)
{
if(a.len < b.len)
return true;
else if(a.len == b.len && a.wei < b.wei)
return true;
else
return false;
}
};
void solve()
{
result = 0;
memset(vis, false, sizeof(vis));
for(int i = 0; i < n; i++)
{
int temp = st[i].wei;
if(vis[i] == true)
continue;
vis[i] = true;
for(int j = i + 1; j < n; j++)
{
if(st[j].wei >= temp && vis[j] != true)
{
vis[j] = true;
temp = st[j].wei;
}
}
result++;
}
}
int main()
{
cin >> T;
while(T--)
{
cin >> n;
for(int i = 0; i < n; i++)
cin >> st[i].len >> st[i].wei;
sort(st, st+n, comp());
solve();
cout << result << endl;
}
return 0;
}