HDU 1789 Doing Homework again

来源:互联网 发布:学生网络欺凌事件 编辑:程序博客网 时间:2024/04/29 01:11

题目:

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

333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 

Sample Output

035
 


大体意思是做作业时每个作业都有时间限制也都有完不成相应的扣分,每个作业都花费一天做完,计算最少扣分是多少。

代码:

#include<cstdio>#include<algorithm>using namespace std;int sum;int search_min(int b[2][1010],int n){int i;int min=b[1][0];int wei=0;for(i=1;i<=n;i++){if(min>b[1][i]){min=b[1][i];wei=i;}}sum=sum+min;return wei;}main(){int i,j,k;int t,n;int shu;int flag;int wei;int a[2][1010],b[2][1010];scanf("%d",&t);for(;t>0;t--){flag=0;sum=0;scanf("%d",&n); for(j=0;j<n;j++){scanf("%d",&a[0][j]);b[0][j]=a[0][j];}for(j=0;j<n;j++){scanf("%d",&a[1][j]);b[1][j]=a[1][j];}for(i=0;i<n;i++){for(j=0;j<n-1;j++){if(b[0][j]>b[0][j+1]){shu=b[0][j];b[0][j]=b[0][j+1];b[0][j+1]=shu;shu=b[1][j];b[1][j]=b[1][j+1];b[1][j+1]=shu;}}}for(i=0;i<n-1;i++){for(j=0;j<n-1;j++){if(b[0][j]==b[0][j+1]){if(b[1][j]>b[1][j+1]){shu=b[1][j];b[1][j]=b[1][j+1];b[1][j+1]=shu;shu=b[0][j];b[0][j]=b[0][j+1];b[0][j+1]=shu;}}}}for(j=0;j<n;j++){if(b[0][j]<=j){wei=search_min(b,j);for(i=wei;i<n;i++){b[0][i]=b[0][i+1];b[1][i]=b[1][i+1];}n--;j--;    }}printf("%d\n",sum);}}

这个题用数组做的,做了一上午。。。

做完了还是不知道贪心算法到底啥意思。。。



0 0
原创粉丝点击