问题描述:
超哥给小凯n条棒子和一个称,让小凯称棒子。超哥告诉小凯每一条棒子的长度L和重量W,但小凯需要用称称一下棒子。每一次开始都要把棒子的W,L重新调整为0,0,每次调零都要花费一块钱。但是小凯享受了折扣,如果他下次称的棒子的W,L都不小于这一次的W,L,那么下一次称量是免费的,现在小凯想知道他最少花几块钱,就能把所有棒子都称一遍。
输入:
每组测试数据第一行一个n,代表几根棒子
接下来一行2n个数,l1,w1,l2,w2,等等,ln,wn。
输出:
输出一行,最少的花费。
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int n;
int ans=0;
int used[100]={0};
//定义结构体变量 棒子的长度和重量
struct Stick{
int length;
int weight;
};
Stick stick[100];
bool compare(Stick x,Stick y) //排序先按长度排序再按重量
{
if(x.length!=y.length) return x.length<y.length;
else return x.weight<y.weight;
}
void solve(); //回溯
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>stick[i].length>>stick[i].weight;
}
solve();
cout<<ans*3<<endl;
return 0;
}
void solve()
{
int nweight; //当前最重的weight
sort(stick,stick+n,compare); //排序
for(int i=0;i<n;i++)
{
if(used[i]==1) continue; //棒子被用过 跳过
else
{
ans++; //算一次钱
nweight=stick[i].weight; //此时最大weight
used[i]=1;
for(int j=i+1;j<n;j++)
{
if(!used[j]&&stick[j].weight>=nweight)
{
nweight=stick[j].weight;
used[j]=1;
}
}
}
}
}