zoukankan      html  css  js  c++  java
  • Doing Homework again

    题目描述

     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 reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

    输入

     The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
    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.

    输出

     For each test case, you should output the smallest total reduced score, one line per test case.

    这题最坑爹的不是贪心算法,而是那个连T都是多组输入;坑到死;

    算了,这个方法来自同学的讲解自己写了一下,错了好多遍(g++)

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct home
    {
    int day;
    int fen;
    } s[1002];
    int cmp(const void*a,const void*b)
    {
    return ((home*)b)->fen-((home*)a)->fen;
    }
    int main()
    {
    int a,t,n,i,bj[1002],sum;
    while(~scanf("%d",&t))
    {
    while(t--)
    {
    sum=0;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    scanf("%d",&s[i].day);
    for(i=0; i<n; i++)
    scanf("%d",&s[i].fen);
    qsort(s,n,sizeof(home),cmp);//按分数排序,不会的可以换成冒泡之类的
    memset(bj,0,sizeof(bj));//将标记数组置0;不会的用for循环;
    for(i=0; i<n; i++)
    {
    a=s[i].day;
    if(bj[a]==0)//判断这一天是否用过;
    bj[a]=1;//没用过就用
    else//用过就向前找天数,因为按分数从高到低排,所以占用前面的也是赚分
    while(a>0)
    {
    if(bj[a])
    a--;
    else
    {
    bj[a]=1;
    break;
    }
    }
    if(a==0)//向前找到0还没找到,说明只能不做了,所以扣分(sum)就要增加一点(s[i].fen)了
    sum+=s[i].fen;
    }
    printf("%d\n",sum);
    }
    }
    return 0;
    }

  • 相关阅读:
    第十九节 集群模式内各节点的通信和文件拷贝
    第十八节 虚拟机克隆后ip修改
    WAF 与 RASP 的安装使用大比拼!
    不改变中间层,如何玩转 .NET 的远程处理功能?
    什么是实时应用程序自我保护(RASP)?
    拒绝「技术栈」选择恐惧症
    为什么Nagios会那么吵?你又能做些什么呢?(1)
    Java 应用发布后,需要关注的7个性能指标
    玩转AWS CloudWatch微信告警
    日均百万 PV 的站点如何做性能监测?试试「3M口罩」!
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3060043.html
Copyright © 2011-2022 走看看