# Doing homework again(贪心)

题目链接:Click here~~

题意:

有 n 门作业,每门作业都有自己的截止期限,当超过截止期限还没有完成作业,就会扣掉相应的分数。问如何才能使扣分最少。

解题思路1:

把 n 门作业按分数从大到小排序,然后每次都把作业安排在离它的截止期限最近的一天(先安排在截止日期当天,如当天已有安排,则往前一天找),并把此天标记为已用,若不能安排,则扣分。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
bool vis[1000];
#define fre freopen("C:\\Users\\Dell\\Desktop\\in.txt", "r", stdin);
struct node{
int dead;
int sub;
bool operator < (const node &a)const{
return a.sub<sub;
}
}stu[1005];
int main(){
//fre;
int t, n, totsub;
cin >> t;
while (t--){
vis[0] = 1;
totsub = 0;
cin >> n;
for (int i = 0; i<n; i++)cin >> stu[i].dead;
for (int i = 0; i<n; i++)cin >> stu[i].sub, totsub += stu[i].sub;
sort(stu, stu + n);
for (int i = 0; i<n; i++){
//int k = 0;
for (int j = stu[i].dead; j >= 1; j--){
if (vis[j] == 0){ vis[j] = 1; totsub -= stu[i].sub; break; }
}
}
cout << totsub << endl;
memset(vis, 0,sizeof(vis) );
}
return 0;
}

解题思路2:

    先对日期从小到大排序,如果日期相同,则扣分多的排在前面。如果相同日期内有扣分多的,则就用前面做扣分少的作业的时间来做这门作业;如果没有比他小的,就扣这门作业的分。On,大大优于前面的算法。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<queue>
#include <bits\stdc++.h>
using namespace std;
#define fre freopen("C:\\Users\\Dell\\Desktop\\in.txt", "r", stdin);
priority_queue<int, vector<int>, greater<int> >q; //小的先出队列
struct in
{
int d, s;//deadline,score
}c[1010];
bool cmp(in a, in b){
return a.d<b.d;
}
int main()
{
fre;
int T, n, i, j, t, cnt, ans;
scanf("%d", &T);
while (T--){
cnt = ans = 0;
t = 1;
while (!q.empty()) q.pop();
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &c[i].d);
for (i = 1; i <= n; i++) scanf("%d", &c[i].s);
sort(c + 1, c + n + 1, cmp);
for (i = 1; i <= n; i++){
//放入从小到大排序的队列,等之后没时间做分值大的作业时,从队头(分值小的作业)开始放弃分数小的作业
q.push(c[i].s);
//如果截止日期相同,也即某一天有不止一门课要交,则一定要从中选择一门放弃,选代价最小的,但是并未决
//定当天要做哪门
if (c[i].d>=t) t++;
else {
ans += q.top(); q.pop();
}
}
printf("%d\n", ans);
}
return 0;
}

# Doing homework again(贪心)的更多相关文章

  1. HDU 1789 Doing Homework again(贪心)

    Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...

  2. hdu--1798--Doing Homework again(贪心)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. hdu 1789 Doing HomeWork Again (贪心算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...

  4. HDU 1789 - Doing Homework again - [贪心+优先队列]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...

  5. HDOJ.1789 Doing Homework again (贪心)

    Doing Homework again 点我挑战题目 题意分析 给出n组数据,每组数据中有每份作业的deadline和score,如果不能按期完成,则要扣相应score,求每组数据最少扣除的scor ...

  6. I - Doing Homework again(贪心)

    I - Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  7. HDU_1789_doing homework again_贪心

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. hdu1789 Doing Homework again(贪心+排序)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题

    题目链接:https://cn.vjudge.net/problem/HDU-1789 题意 小明有一大堆作业没写,且做一个作业就要花一天时间 给出所有作业的时间限制,和不写作业后要扣的分数 问如何安 ...

  10. 【HDOJ6343】Graph Theory Homework(贪心)

    题意: 给定n个点,每个点有权值a[i],从A走到B的花费是下取整sqrt(a[i]-a[j]),求从1号点走到n号点的最小花费 1<=n,a[i]<=1e5 思路: #include&l ...

随机推荐

  1. iOS开发之实现图片自动切换(类似android画廊效果)

    #import ViewController.h #define ImageViewCount 5   @interface ViewController ()<uiscrollviewdele ...

  2. Codeforces 785 D.Anton and School - 2(组合数处理)

    Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...

  3. ARTS打卡计划第五周

    Algorithms: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 采用了map的 ...

  4. 阿里云服务器ECS装好宝塔 但访问不了面板的解决方法

    (SSH) (phpmyadmin) 如果你进入面板里修改了面板端口或FTP端口,记得要在安全组和面板防火墙放行相应端口

  5. Throwable 源码阅读

    Throwable 属性说明 /** * Java 语言中所有错误和异常的基类,此类及其子类才能通过 throws 被 JVM 虚拟机抛出. * @since 1.0 */ public class ...

  6. 【React自制全家桶】八、React动画以及react-transition-group动画库的使用

    React动画通常有三种方法实现从易到难为: 1.transition(CSS3自带) 2.animation(CSS3自带) 3.react-transition-group动画库(需要引入插件) ...

  7. CSS中盒子模型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 【汇总】数据库提权(mysql、mssql)

    日期:2018-04-03 11:46:45 作者:Bay0net 介绍:利用 mssql 的 sa 账号提权.利用 MySQL 的 UDF 提权 0x01.mssql 提权 恢复 xp_cmdshe ...

  9. IPV6测试方法

    终端 dig +nocmd + nostats 你的域名 AAAA: 查看Got answer 如果 status的状态是NO ERROR 那就是支持IPV6 就没啥问题. 如果status 的状态是 ...

  10. 基于Bootstrap 3可预览的HTML5文件上传插件

    前端常用资源地址: http://www.htmleaf.com/ http://www.htmleaf.com/html5/html5muban/201505091801.html 源代码地址 ht ...