I - Doing Homework again(贪心)

I - Doing Homework again

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

Description

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

//又坑到我了,唉,贪心算法。我想的是按交作业的天数从小到大排,天数一样就扣分多的放前面,然后开始遍历,如果不能完成,看前面有没有分数比这个小的,有就扣那个最少的分,注意是最少的,没有就扣这个分,然后直到遍历结束,求告诉哪里错了。

 #include <iostream>
#include <algorithm>
using namespace std; struct zuoye
{
int day;
int fen;
};
zuoye book[]; int cmp(zuoye a,zuoye b)
{ if (a.day!=b.day)
return a.day<b.day;
return a.fen>b.fen;
} int main()
{
int n,i,all,cur,min,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for (i=;i<n;i++) scanf("%d",&book[i].day);
for (i=;i<n;i++) scanf("%d",&book[i].fen);
sort(book,book+n,cmp);
cur=,all=;
for (i=;i<n;i++)
{
if (cur<=book[i].day)
{
cur++;
continue;
}
if (cur>book[i].day)
{
min=book[i].fen;
for (int j=i-;j>=;j--)
{
if (book[j].fen<min)
{
min=book[j].fen;
}
}
all+=min;
}
}
printf("%d\n",all);
}
return ;
}

AC的:看别人的,是按分数大的排前面,若分一样,天数小的靠前。遍历每一个数据,将截至日期前的天数再遍历,如果有空闲的日子,就用那天做这个,没有就扣分

 #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; struct Node
{
int day,fen;
} node[]; int cmp(struct Node a,struct Node b)
{
if(a.fen!=b.fen)
return a.fen > b.fen;//扣分越多的越靠前
return a.day < b.day;//扣分相同的时候,deadline越早的越靠前
} int visit[];//如果当天没用过,值为0;否则为1 int main()
{
int T,n,i,j,all;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (i=;i<n;i++) scanf("%d",&node[i].day);
for (i=;i<n;i++) scanf("%d",&node[i].fen);
memset(visit,,sizeof(visit));
sort(node,node+n,cmp);
all=;
for (i=;i<n;i++)
{
for(j=node[i].day;j>;j--)
{
if (!visit[j])
{
visit[j]=;
break;
}
}
if (j==) all+=node[i].fen;
}
printf("%d\n",all); } return ;
}
上一篇:android 屏幕尺寸的理解


下一篇:一 Hive安装及初体验