hdu--1798--Doing Homework again(贪心)
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13847 Accepted Submission(s): 8036
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
/*
Name: hdu--1798--Doing Homework again
Copyright: 2017 日天大帝
Author: 日天大帝
Date: 21/04/17 15:32
Description: 贪心,思路让当前分数大的替换当前分数小的作业
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct work{
int score,deadline;
bool operator<(const work &a)const{
return score>a.score;
}
}arr[];
bool cmp(work a,work b){
return a.deadline<b.deadline;
}
priority_queue<work> q;//按照分数排序
int main(){
ios::sync_with_stdio(false);
int T;cin>>T;
while(T--){
memset(arr,,sizeof(arr));
while(!q.empty())q.pop();
int n;cin>>n;
; i<n; ++i)cin>>arr[i].deadline;
; i<n; ++i)cin>>arr[i].score;
,ans = ;
sort(arr,arr+n,cmp);//按照时间排序
; i<n; ++i){
q.push(arr[i]);
if(t < arr[i].deadline){
t++;continue;
}
ans += q.top().score;
q.pop();
}
cout<<ans<<endl;
}
;
}
/*大神的代码,优先队列和排序很巧妙*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <utility>
#include <vector>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
<< ;
const int INF = 0x3f3f3f3f;
P arr[MAX_N];
int main() {
//ios::sync_with_stdio(false);
//cin.tie(NULL);
//cout.tie(NULL);
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
; i < n; ++i)
scanf("%d", &arr[i].first);
; i < n; ++i)
scanf("%d", &arr[i].second);
sort(arr, arr + n);
, day = ;
priority_queue<int, vector<int>, greater<int> > pque;
; i < n; ++i) {
pque.push(arr[i].second);
if (day < arr[i].first) {
++day;
continue;
}
ans += pque.top();
pque.pop();
}
printf("%d\n", ans);
}
;
}
hdu--1798--Doing Homework again(贪心)的更多相关文章
- hdu 1789 Doing HomeWork Again (贪心算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...
- HDU 1789 - Doing Homework again - [贪心+优先队列]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDU 1789 Doing Homework again(贪心)
Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- HDU 1789 Doing Homework again(非常经典的贪心)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 题解报告:hdu 1789 Doing Homework again(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has just come back ...
- HDU 1789 Doing Homework again (贪心)
Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...
- HDU 1789 Doing Homework again(贪心)
在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...
随机推荐
- Mybatis中使用 #{} 和 ${} 向sql传参时的区别
今天在工作时,使用MyBatis中向sql传递两个参数时,一直显示SQL语法错误,仔细检查,才发现传入的参数被加上了引号,导致传入的参数(要传入的参数是表名)附近出现语法错误. 错误写法: } a } ...
- JAX-RS REST 服务结果的自动封装
如转发请注明: 原文luyiisme博客 当使用遵循 JAX-RS 标准的框架开发REST 服务时,我们倾向于定义个(含有JAX-RS)注解接口. 服务器端负责实现该接口,而客户端是该接口的代理进行远 ...
- Python将数据插入到数据库时遇到单引号插入错误的问题
这才是真正的解决方法,真不知道有些人连试都没试过就乱转载 比如你要插入一个字符串,是一个变量 如:str = "I'am a handsom boy" 由于这个字符串包含',插入数 ...
- flask 上传头像
上传头像,自己感觉了好久,就是上传文件呗其实,存在一个路径,数据库存储这个路径,然后展示给前端,啥都不说,看怎么实现的. 数据库设置如下 user_image=db.Column(db.String( ...
- python迭代器生成器(一)
for循环可以用于python中任何序列类型,包括序列.元组以及字符串.例如: >>> for x in [1,2,3,4]: print(x * 2,end='')...2468 ...
- AJAX跨域的常见方法
由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...
- HDOJ 2209 贪心
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 基于Bootstrap+angular的一个豆瓣电影app
1.搭建项目框架 npm初始化项目 npm init -y //按默认配置初始化项目 安装需要的第三方库 npm install bootstrap angular angular-route --s ...
- Mac之OS系统下搭建JavaEE环境 <三> 之Eclipse 与 IntelliJ IDEA 的安装配置
三.Eclipse 与 IntelliJ IDEA 的安装配置 1.Eclipse的安装配置 1 下载eclipse 到官网 https://www.eclipse.org/downloads/ecl ...
- Jenkins构建Android项目持续集成之单元测试及代码覆盖率
单元测试 在软件开发中一直在推崇TDD(测试驱动开发),但是一直不能被有效的执行或者并不是真正的测试驱动开发(先开发后写单元测试),因为我们懒!而Android开发又是大多应用层面的开发,很多都是和视 ...