P1233 木棍加工 题目链接:https://www.luogu.org/problem/P1233
参考博客:题解区第二个
#include <bits/stdc++.h>
using namespace std;
const int maxn=5e3+1;
int n,sum,temp;
struct thing
{
int lo,wi;
}t[maxn];
bool used[maxn];
bool comp(thing &a,thing &b)
{
if(a.lo==b.lo)return a.wi>b.wi;
return a.lo>b.lo;
}
int main()
{
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>t[i].lo>>t[i].wi;
}
sort(t+1,t+n+1,comp);
for(int i=1;i<=n;i++)
{
if(used[i]==0)
{
temp=t[i].wi;
for(int j=i+1;j<=n;j++)
{
if(t[j].wi<=temp&&used[j]==0)
{
used[j]=1;
temp=t[j].wi;
}
}
}
}
for(int i=1;i<=n;i++)
{
if(used[i]==0)
{
sum++;
}
}
cout<<sum<<endl;
return 0;
}
P1103 书本整理 题目链接:https://www.luogu.org/problem/P1103
参考博客:https://www.luogu.org/blog/user8742/solution-p1103
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e2+1,p=1e9;
pair<int,int>a[maxn];
int n,k,ans=p,f[maxn];
int main()
{
ios::sync_with_stdio(false);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].first,&a[i].second);
}
sort(a+1,a+n+1);
for(int j=2;j<=n-k;j++)
for(int i=n;i>=j;i--)
{
f[i]=p;
for(int l=j-1;l<=i-1;l++)
{
f[i]=min(f[l]+abs(a[i].second-a[l].second),f[i]);
}
}
for(int i=n-k;i<=n;i++)
ans=min(ans,f[i]);
printf("%d",ans);
return 0;
}