HDU1789Doing Homework again(贪婪)

HDU1789Doing Homework again(贪心)

题目链接

题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数。问如何安排能够使得扣分最少。

解题思路:贪心,将扣分多的作业排在前面,扣分同样的依照最后期限前的排前面,然后用一个数组来表示第i天是否有安排。每次都将第i个作业放到它的最后期限的那天完毕,但假设这一天被占了,那么就仅仅能往前移动,找空暇的天。假设一直找到了0。那么说明这个作业是无法按时完毕了,就加上分数。假设某项作业完毕的最后期限比n还大,那么这个作业一定是能够及时完毕的,那么就能够无论它了。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e3 + 5;
int vis[maxn];

int n;
struct homework {

    int deadt, score;
}h[maxn];

int cmp (const homework &a, const homework &b) {

    if (a.score != b.score)
        return a.score > b.score;
    return a.deadt < b.deadt;
}

int solve () {

    sort(h, h + n, cmp);
    memset (vis, -1, sizeof (vis));

    int ans = 0, time;
    for (int i = 0; i < n; i++) {

        time = h[i].deadt - 1;
        if (time >= n)
            continue;
        while (time >= 0 && vis[time] != -1) {
            time--;
        }

        if (time >= 0)
            vis[time] = 1;
        else
            ans += h[i].score;
    }
    return ans;
}

int main () {

    int T;
    scanf ("%d", &T);
    while (T--) {

        scanf ("%d", &n);
        for (int i = 0; i < n; i++)
            scanf ("%d", &h[i].deadt);
        for (int i = 0; i < n; i++)
            scanf ("%d", &h[i].score);

        printf ("%d\n", solve());
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-08-02 10:54:32

HDU1789Doing Homework again(贪婪)的相关文章

hdu1789Doing Homework again(贪心)

题目链接: 啊哈哈,点我点我 思路: 这道题是简单的贪心..先按分数从大到小排序,然后将这个分数的截止日期从后向前扫描,如果碰到没有被标记的则这一天可以做这个作业... 题目: Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6451    Accepted Submission(s): 383

ZOJ 2109 FatMouse&amp;#39; Trade (背包 dp + 贪婪)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

HDU 1789 Doing Homework again(贪心)

Doing Homework again Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadlin

uva 1489 - Math teacher&#39;s homework(数位dp)

题目链接:uva 1489 - Math teacher's homework 题目大意:给定n,k,以及序列m1,m2,-,mn, 要求找到一个长度为n的序列,满足0<=xi<=mi, 并且x1XORx2XOR-XORxn=k 解题思路:数位dp,在网上看了别人的代码,高大上... 假设有二进制数 k : 00001xxxx mi:0001xxxxx, 那么对于xi即可以满足任意的x1XORx2XOR-XORxi?1XORxi+1XOR-XORxn,根据这一点进行数位dp. dp[i][j]

python正则非贪婪模式

上一篇python正则匹配次数大家应该也发现了,除了?其他匹配次数规则都是尽可能多的匹配 那如果只想匹配1次怎么办呢,这就是正则中非贪婪模式的概念了 原理就是利用?与其他匹配次数规则进行组合 +?  *?  {m,n}?等就暂不举例了

AC日记——贪婪大陆 洛谷 P2184

贪婪大陆 思路: 树状数组: 跪烂.. 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 int n,m,ltree[maxn],rtree[maxn],tot; inline void in(int &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0')Cget=getchar(); while(Cget>='0

python 正则贪婪模式和非贪婪模式

贪婪模式:在表达式匹配成功的前提下,总是尽可能多的匹配字符飞天蓝模式:在表达式匹配成功的前提下,总是尽量少的匹配字符 1 # !/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import re 4 5 def test(): 6 string = "abc123456789sdsddskcas" 7 print "a(.*)c贪婪模式匹配结果:" 8 print re.findall(r"a(.*)c&quo

HDU 1074 Doing Homework 状压DP

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

HDU 1789 Doing Homework again

在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案直接交换就可以了. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct HomeWork { int de