Description
In HUST,there are always many students go to the mell hall at the same time as soon as the bell rings. Students have to queue up for a meal ,and the queue is awalys long,So it takes much time.Suposse there are N people in a queue,each person has two characteristic value A and B(both of them are integers,read input for more details),the i-th person in the queue have to spend m(i)=A[1]*A[2]*…A[i-1]/B[i] minutes,Where A[i],B[i] is the i_th person’s value A,B.Notice that if the order of the queue changes,the waiting time one spend(that is,the value of m(i))may changes too. Of course,every student want to reduce the time he spend.
Apparently,it is impossible to make everyone satisfied,in this problem,we only need to minimize the waiting time of one who spend the longest time in the queue,that is,minimize Max{ m(1),m(2),…,m(n)}.You can change the order of the queue in anyway in order to complete this problem.
You are asked to output the original location in the queue of the person who will cost the longest time under optimal solution. Uniquity is insured by the given data.
Input
There are multiple test cases.
For each case, the first line contains one integer N(1≤N≤1000).
The second line contains N integers A[i].(0<A[i]<100000)
The third line contains N intergers B[i](0<B[i]<10).
(B[i] < 10 < A[i]*b[i]) (Please pay attention to the range, it is useful on this problem)
Output
You are asked to output the original location in the queue of the person who will cost the longest time under optimal solution.
Uniquity is insured by the given data.
Sample Input
3 15 20 25 1 3 2
Sample Output
2
题目有个巨大的陷阱,其实不是用动态规划做的。m(i)=A[1]*A[2]*…A[i-1]/B[i] ,只要A[i]*B[i]最大,那么他就一定排在最后一位,而最后一位等
待的时间最长,所以若要最小化等待时间最长的人的时间,一定要最大化A[i]*B[i]的值
其实也应该早注意到题目中的一句话“(B[i] < 10 < A[i]*b[i]) (Please pay attention to the range, it is useful on this problem)”
其实不用排序的,只要找到最大值就好了,我只是讲错误的代码改的时候方便了下,这样还是很耗时的==
1 # include<stdio.h> 2 # include<stdlib.h> 3 int n; 4 struct node{ 5 int x,y,num; 6 int f; 7 }s[1005]; 8 int cmp(const void *a,const void *b) 9 { 10 struct node * aa =(node *)a; 11 struct node * bb=(node *)b; 12 return bb->f - aa->f; 13 } 14 int main() 15 { 16 int i; 17 while(scanf("%d",&n)!=EOF) 18 { 19 for(i=0;i<n;i++) 20 scanf("%d",&s[i].x); 21 for(i=0;i<n;i++) 22 { 23 scanf("%d",&s[i].y); 24 s[i].f=s[i].x*s[i].y; 25 s[i].num=i+1; 26 } 27 qsort(s,n,sizeof(s[0]),cmp); 28 printf("%d\n",s[0].num); 29 } 30 return 0; 31 }