现在的位置: 首页 > 综合 > 正文

Doing Homework again

2019年11月09日 ⁄ 综合 ⁄ 共 2215字 ⁄ 字号 评论关闭
Doing Homework again

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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 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.
 

Input

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.
 

Output

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

Sample Input

3 3 3 3 3 10 5 1 3 1 3 1 6 2 3 7 1 4 6 4 2 4 3 3 2 1 7 6 5 4
 

Sample Output

0 3 5
题目大意:第一行输入为测试组数,第二行输入为有多少科科目,第三行是各科的上交作业的截止时间,第四行是如果没有在截止时间内上交,各科的惩罚值,因为截止时间可能在同一天,所以题目就要求,尽可能的减少惩罚值
思路:按截止时间从小到大排序,如果截止时间相同,就按惩罚值从大到小排序
用测试数据3举例:
排序后变为
1 3
2 6
3 4
4 7
4 5
4 2
6 1
int k=0,use=0;
如果k<截止时间,k++,并标记该元素已经用了,use=1;
for循环到4 5 的时候,k=4,这时候k!<截止时间,则去前面寻找,前面标记过得元素中,有没有惩罚值比这个小的,
比如现在 1 3 的惩罚值就比4 5的惩罚值小,肯定就选用4 5,而不用1 3,所以标记1 3 use=0,4 5 use=1;
以此类推,最后被选的是2 6;3 4;4 7; 4 5; 6 1;所以没做的只有1 3;4 2;惩罚值最小为5
#include<stdio.h> #include<stdlib.h> struct homework {     int e;     int p; int use; }a[1010]; int cmp(const void *a,const void*b) {     struct homework*c=(homework*)a;     struct homework*d=(homework*)b;     if(c->e!=d->e)return c->e-d->e;     else return d->p-c->p; } int search(homework a[1010],int p,int s) { int i; int t=-1; for(i=0;i<s;i++) { if(a[i].use==1&&a[i].p<p) { p=a[i].p; t=i; } } return t; } int main() {     int n,i,t;     scanf("%d",&t); while(t--) { scanf("%d",&n);         for(i=0;i<n;i++)         { scanf("%d",&a[i].e); a[i].use=0;         }         for(i=0;i<n;i++) {             scanf("%d",&a[i].p);         } qsort(a,n,sizeof(a[0]),cmp); int k=0,h=0; for(i=0;i<n;i++) { if(k<a[i].e)//截止时间不同的每一天进行处理 { k++; a[i].use=1;//用了的标记为1 } else//截止时间相同的 { h=search(a,a[i].p,i);//查找能否可以交换 // printf("%d\n",h); if(h!=-1)//可以交换 { a[i].use=1; a[h].use=0; } } } /* for(i=0;i<n;i++) { printf("%d %d %d\n",a[i].e,a[i].p,a[i].use); }*/ int sum=0; for(i=0;i<n;i++) { if(a[i].use!=1) sum+=a[i].p; } printf("%d\n",sum); } return 0; }
 
【上篇】
【下篇】

抱歉!评论已关闭.