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;
    }

  • 相关阅读:
    关于C#静态构造函数的几点说明
    《JavaScript高级程序设计》读书笔记之一:几个与原始类型等价的引用类型的常用方法和属性
    Ajax 简介
    如何优化JavaScript脚本的性能
    摆脱混沌,建立个人能力体系——病症四起【from csdn】
    JMX理解与实例
    苹果电脑不为人所知的第三个创始人
    一些AS3中常用到的公式
    JavaScript 随笔
    解决IE6 JSONP无响应的问题。
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3060043.html
Copyright © 2011-2022 走看看