HDU1789 Doing Homework again(贪心)
2016-04-21 18:52
429 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1789
题目大意:在给定的作业扣分和截止时间中找出最少的扣分。解题思路:由贪心原则,现将数据排序,即按照所扣分数最大->最小排序(如果分数相同则按照截止日期从小到大排序)(cmp()),接下来从1->n 然后按顺序,从截止日期开始往前找没有占用掉的时间。如果找不到了,则加到罚分里面#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
struct node
{
int time;
int score;
bool mark;
}nod[1005];
int day[1005];
bool cmp1(node a,node b)
{
if(a.score==b.score)return a.time<b.time;
return a.score>b.score;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,n,ans;
cin>>t;
while(t--)
{
ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>nod[i].time;
///nod[i].mark=false;
}
for(int j=1;j<=n;j++)
{
cin>>nod[j].score;
}
sort(nod+1,nod+1+n,cmp1);
int j;
memset(day,0,sizeof(day));
for(int i=1;i<=n;i++)
{
for(j=nod[i].time;j>=1;j--)
{
if(!day[j]){day[j]=1;break;}
}
if(j==0)ans+=nod[i].score;
}
cout<<ans<<endl;
}
return 0;
}
测试数据:score 7 6 5 4 3 2 1 time 4 2 4 3 1 4 6 ans 5 本来应该是用 DP 解了, 毕竟贪心只能解决局部最优,而 DP 解决的是全局最优,就像浩神说的一样,一切问题都可以转换为DP 但是 DP 实在是太弱了,这题数据比较小贪心两次循环就可以解决
题目大意:在给定的作业扣分和截止时间中找出最少的扣分。解题思路:由贪心原则,现将数据排序,即按照所扣分数最大->最小排序(如果分数相同则按照截止日期从小到大排序)(cmp()),接下来从1->n 然后按顺序,从截止日期开始往前找没有占用掉的时间。如果找不到了,则加到罚分里面#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
struct node
{
int time;
int score;
bool mark;
}nod[1005];
int day[1005];
bool cmp1(node a,node b)
{
if(a.score==b.score)return a.time<b.time;
return a.score>b.score;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,n,ans;
cin>>t;
while(t--)
{
ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>nod[i].time;
///nod[i].mark=false;
}
for(int j=1;j<=n;j++)
{
cin>>nod[j].score;
}
sort(nod+1,nod+1+n,cmp1);
int j;
memset(day,0,sizeof(day));
for(int i=1;i<=n;i++)
{
for(j=nod[i].time;j>=1;j--)
{
if(!day[j]){day[j]=1;break;}
}
if(j==0)ans+=nod[i].score;
}
cout<<ans<<endl;
}
return 0;
}
测试数据:score 7 6 5 4 3 2 1 time 4 2 4 3 1 4 6 ans 5 本来应该是用 DP 解了, 毕竟贪心只能解决局部最优,而 DP 解决的是全局最优,就像浩神说的一样,一切问题都可以转换为DP 但是 DP 实在是太弱了,这题数据比较小贪心两次循环就可以解决
相关文章推荐
- 爬楼梯的问题Climbing Stairs
- 十、责任链模式Chain of Responsibility(行为型模式)
- http://blog.csdn.net/holybin/article/details/46955185
- 服务器端主动关闭连接, 产生的TIME_WAIT状态为什么会占用服务端大量端口?
- HoloLens开发手记-硬件细节 Hardware Detail
- git修改提交的用户名和Email
- 03-树3 Tree Traversals Again
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 正确使用Block避免Cycle Retain和Crash
- lightoj 1076 - Get the Containers 【二分 + 贪心】
- Failed to read artifact descriptor进行安装maven项目出错
- AI编辑SVG格式的相关问题
- paip.自适应网页设计 跟 响应式 设计的区别跟原理and实践总结
- 图解RAID 0, RAID 1, RAID 5, RAID 10
- go gomail
- R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)
- kaidi中 install cuda
- stl map用法和make_pair函数
- 关于解决 Failed to prepare partial IU:
- HDU 1848 Fibonacci again and again SG函数详解!