Question L: Robots
Time Limit: 1 Sec Memory Limit: 128 MBCommits: 164 Resolved: 65
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]
Topic description
You’re the leading designer of a complex robot, that will explore human unreachable locations. Your job is to design a robot that will go as far as possible. To do this, you have n available energy sources. The ith source is capable of accelerating the robot by a rate of ai (m/s2) and can do this for a total of si seconds. The robot is initially at rest (its initial velocity is zero).
You have to decide the order in which to use the sources in order to maximize the total distance traveled by the robot. You will use one source until si seconds have elapsed, then immediately switch to another unused source (the switch is instantaneous). Each source can be used only once.
Given the accelerations and durations of each source, write an efficient program to determine the optimal order of the sources, in order to maximize the total distance traveled. Your program must compute the difference between the traveled distance in the optimal case and in the default case (the order given by the input data).
Physics background: if the velocity is v before you start using a source whose acceleration is a then, after t seconds, the robot has traveled a total vt+1/2at2 meters, and the final velocity will be v' = v+at.
You have to decide the order in which to use the sources in order to maximize the total distance traveled by the robot. You will use one source until si seconds have elapsed, then immediately switch to another unused source (the switch is instantaneous). Each source can be used only once.
Given the accelerations and durations of each source, write an efficient program to determine the optimal order of the sources, in order to maximize the total distance traveled. Your program must compute the difference between the traveled distance in the optimal case and in the default case (the order given by the input data).
Physics background: if the velocity is v before you start using a source whose acceleration is a then, after t seconds, the robot has traveled a total vt+1/2at2 meters, and the final velocity will be v' = v+at.
enter
The input file starts with the number n (1 ≤ n ≤ 104) of sources. Starting from a different line follows the n space-separated acceleration and duration for each source (positive integer numbers).
output
The output file contains the computed difference between the traveled distance in the optimal case and in the default case (the order given by the input data), with one decimal.
sample input
2
2 1
30 2
Sample output
56.0
************************************************ **************************************************** ********************************
Greedy water question is just for a record
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; struct x { int a; int t; }; int cmp(x p,x q) { return p.a>q.a; } intmain () { int n; x a[10005]; x b[10005]; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d %d",&a[i].a,&a[i].t); b[i].a = a[i].a; b[i].t = a[i].t; } sort(a,a+n,cmp); double ans = 0; double x1 = 0; double x2 = 0; double v1 = 0; double v2 = 0; for(int i=0;i<n;i++) { x1 = v1*a[i].t+(1.0/2)*(a[i].a)*(a[i].t)*(a[i].t); v1 = v1+a[i].a * a[i].t; x2 = v2*b[i].t+(1.0/2)*(b[i].a)*(b[i].t)*(b[i].t); v2 = v2+b[i].a * b[i].t; ans += x1-x2; // Because I saw that many people have wa and are afraid of exceeding the limit, I choose to see each time instead of subtracting at the end } printf("%.1f",ans); }