HDU 很老的一道题 有没有大佬有思路的。。。求助

一百个Chocolate
前端领域优质创作者
博客专家认证
2019-07-30 11:24:54
Problem Description
ACM is popular in HDU. Many girls want to learn more about programming skills in ACM. As one of assistances of Lcy, Yifenfei is now busy preparing a new club called “MM Programming Club”. Of Course, He will be the leader of the club, and teach these girls patiently. After the news posted around the campus, as many as N girls are determined to take part in the club. However, the numbers of members are limited; Yifenfei will only select K of them. It is quite a difficult problem. Here is a list of all information about N girls. Each of them has intelligence value and prettiness value. He also wants these K members such that the difference of intelligence between any two of them must not be greater than MAXK (If K = 1, the difference is zero). Now he wants to maximize the Sum of these K girls’ prettiness value.
Input
Many test case, please process to end of file. Each test first contains three integers N(1 <= N <= 200), K(1 <= K <= N), MAXK(1 <= MAXK <= 500). Then N lines follow. Each line contains two integers S, T (1 <= S, T <= 500). S represents the intelligence value, and T represents the prettiness value.
Output
If he can’t succeed in selecting K girls, print “-1”. Otherwise, Print the maximum the Sum of these K girls’ prettiness value.
Sample Input

2 1 0
1 2
2 3
2 2 0
1 2
2 3
2 2 1
1 2
2 3

Sample Output

3
-1
5
...全文
274 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
过了,采用贪心思想,先按照智力从小到大排序,可以这样理解,最优解中肯定有一个人智力最小,所以我们轮流让每个人做智力最小的人,肯定能找到最优解 然后每次取k-1个人,不断找,找到k-1个人的美貌值都是最大的 #include<iostream> #include<cstring> #include<cmath> #include<vector> #include<algorithm> #define mst(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn=10000+5; struct node{ int w,v; }stu[maxn]; bool cmp1(node x,node y) { return x.w<y.w; } bool cmp2(node x,node y) { return x.v>y.v; } int n,k,limit,res,ans; int main() { ios::sync_with_stdio(false); cin.tie(0); while(cin>>n>>k>>limit) { res=-1; for(int i=1;i<=n;i++) cin>>stu[i].w>>stu[i].v; sort(stu+1,stu+n+1,cmp1); for(int i=1;i<=n-k+1;i++) { ans=stu[i].v; int w=stu[i].w; vector<node> tmp; for(int j=i+1;stu[j].w-w<=limit&&j<=n;j++) tmp.push_back(stu[j]); if(tmp.size()<k-1) continue; sort(tmp.begin(),tmp.end(),cmp2); for(int m=0;m<k-1;m++) ans+=tmp[m].v; res=max(res,ans); } cout<<res<<endl; } return 0; }
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
引用 8 楼 Italink 的回复:
这样的最优解问题都可以用动态规划来做
能详细一点吗 可以请教代码吗 我同学用贪心做出来了,给我提了思路 我现在还在做 做出来了贴代码
Italink 2019-07-30
  • 打赏
  • 举报
回复
这样的最优解问题都可以用动态规划来做
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
/*这是我采用dfs做的 一个一个搜 样例都过了 但是TLE了 卡时间了*/ #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #define mst(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn=600+5; struct node{ int w,v; }stu[maxn]; int n,k,limit,res,cnt,ans; int vis[maxn]; bool is_OK(int t) { for(int i=1;i<=n;i++) { if(vis[i]) { if(abs(stu[i].w-stu[t].w)>limit) return false; } } return true; } void dfs(int t) { if(k==cnt) res=max(res,ans); if(t>n) return; for(int i=1;i<=n;i++) { if(!vis[i]) { if(is_OK(i)) { vis[i]=1; ++cnt; ans+=stu[i].v; dfs(t+1); ans-=stu[i].v; --cnt; vis[i]=0; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); while(cin>>n>>k>>limit) { res=0,cnt=0,ans=0; mst(vis,0); for(int i=1;i<=n;i++) { cin>>stu[i].w>>stu[i].v; } dfs(1); if(res) cout<<res<<endl; else cout<<-1<<endl; } return 0; }
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
/*目前wa了一发的代码 */ #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #define mst(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn=600+5; struct node{ int w,v; }stu[maxn]; int n,k,limit,res,cnt; int vis[maxn]; bool cmp(node x,node y) { return x.v>y.v; } bool is_OK(int t) { for(int i=0;i<n;i++) { if(vis[i]) { if(abs(stu[i].w-stu[t].w)>limit) return false; } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); while(cin>>n>>k>>limit) { res=0,cnt=0; mst(vis,0); for(int i=0;i<n;i++) { cin>>stu[i].w>>stu[i].v; } sort(stu,stu+n,cmp); for(int i=0;i<n;i++) { if(!vis[i]) { if(is_OK(i)) vis[i]=1; } } for(int i=0;i<n;i++) { if(vis[i]) cnt++; } if(cnt==k) { for(int i=0;i<n;i++) { if(vis[i]) res+=stu[i].v; } } if(res) cout<<res<<endl; else cout<<-1<<endl; } return 0; }
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
引用 4 楼 早打大打打核战争 的回复:
输入 排序 比较 输出
能不能请教一下代码 假设有4个人 从中选2个 最后的结果可能不会选美貌值最高的那个 可能美貌值高但是智力和其他的相差太大 导致我们选不到两人
  • 打赏
  • 举报
回复
输入 排序 比较 输出
一百个Chocolate 2019-07-30
  • 打赏
  • 举报
回复
ACM在HDU中很流行。许多女孩想在ACM中学习更多的编程技巧。在Lcy的帮助下,Yifenfei现在正忙着筹备一个新的俱乐部,叫做“MM编程俱乐部”。当然,他会是俱乐部的领导,耐心地教这些女孩子。消息发布在校园里后,多达N名女生决定参加这个社团。但是,成员数目有限;Yifenfei只会选择其中的K个。这是一个相当困难的问题。这是一个关于N个女孩的所有信息的列表。它们都具有智力价值和审美价值。他还希望这K个成员之间的智力差异不大于MAXK(如果K = 1,那么这个差异为零)。现在他想最大化K个女孩的美貌值之和。 输入 许多测试用例,请处理到文件末尾。每个测试首先包含三个整数N(1 <= N <= 200), K(1 <= K <= N), MAXK(1 <= MAXK <= 500)。然后是N行。每一行包含两个整数S, T (1 <= S, T <= 500)。S表示智力值,T表示美貌值。 输出 如果他不能成功地选择K个女孩,打印“-1”。否则,打印这K个女孩的美貌值之和的最大值。
  • 打赏
  • 举报
回复
凡是英文帖子统统不要邀请我,谢谢!
  • 打赏
  • 举报
回复
英文一窍不通看不懂

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧