Title: Residents ' rally
Blue Bridge Village residents are living on the edge of a highway, the length of the road is L, each family's location is used to the distance from the road to the beginning of the family to calculate, the first family from the starting point of di. Every year, Blue Bridge village has to hold a rally. This year, due to the large population of the village, the villagers ' committee decided to hold rallies in 4 places, of which 3 were located in the middle of the road and 1 at the end of the road.
It is known that each family will go to a rally away from the start of the road and attend a rally
The cost of the journey is the number of people in the family TI and distance product.
Given the location of each family Di and the number of TI, please find the best meeting place for the Village committee
: P1, P2, p3, P4 (p1<=p2<=p3<=p4=l), so that all the people in the village drive away
Pin and minimum.
"Input Format"
The first line of input contains two integers n, L, respectively, representing the number of families of Blue Bridge Village and the length of the road
Degree.
Next n lines, two integers per line, Di, TI, respectively, representing the family of I households from the road starting point
Distance and the number of people in the family.
"Output Format"
The output line contains an integer that represents the cost of all people in the village and the distance.
"Sample Input"
6 10
1 3
2 2
4 5
5 20
6 5
: U
"Sample Output"
18
"Sample description"
In the distance from the beginning 2, 5, 8, 10 of these 4 local gatherings, 6 families need walking distances respectively
For 1, 0, 1, 0, 2, 0, the total distance cost is
1*3+0*2+1*5+0*20+2*5+0*7=18.
"Data size and conventions"
For 10% of the evaluation data, 1<=n<=300.
For 30% of the evaluation data, 1<=N<=2000,1<=L<=10000,0<=DI<=L,
Di<=di+1,0<=ti<=20.
For 100% of the evaluation data, 1<=N<=100000,1<=L<=1000000,0<=DI<=L,
di<=di+1,0<=ti<=1000000.
Resource contract:
Peak memory Consumption < 512M
CPU Consumption < 5000ms
Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Of
Superfluous content.
All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.
Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, do not invoke dependent on the compilation environment or
As a special function of the system.
Note: All dependent functions must be explicitly #include <xxx> in the source file;
Can omit common header files through engineering setup.
When committing, be careful to choose the type of compiler you expect.
Very hard to work out:
The idea is that the first three meeting places must be built in a person's home, this may be more time-saving, this interval is divided into four parts, and then each section with a for loop run, respectively named Interval 1, Interval 2, Interval 3, interval 4;
Interval 1--4:S1 is "1,i", S2 is "i+1,j", S3 is "J+1,k", S4 is "k+1,n+1"; (N+1 is where the end of the road is)
problems with optimization:
Run for Loop Time said the explosion is fried, can be pretreated, in the O (n) time range of processing out, and finally in the run can be appropriate to cut off some situations, such as S1 or S1+S2 if it has been more than ans continue.
Note: The following num[i] represents the total population of 1--i families.
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <vector>5#include <math.h>6#include <algorithm>7#include <Set>8#include <string.h>9#include <string>Ten#include <map> One#include <queue> A using namespacestd; - #defineN 100008 - #definell Long Long the #defineull unsigned long Long - #defineINF 0x3f3f3f3f - structnode{ - ll Dis,num; + }p[n]; -ll Num[n],d[n];//D[i] = distance from p[i] to P[i-1] + ll S4[n]; A at intMain () { - intN; - ll L; -scanf"%d%i64d",&n,&L); - intx, y; -num[0]=0LL; in for(intI=1; i<=n;i++){ -num[i]=0LL; toscanf"%i64d%i64d",&p[i].dis,&p[i].num); +num[i]=num[i-1]+P[i].num; - if(i==1) d[i]=0; the Elsed[i]=p[i].dis-p[i-1].dis; * } $p[n+1].dis=l;p[n+1].num=0;Panax Notoginseng if(n<=3){ -printf"0\n"); the } + Else{ As4[n+1]=0; the for(inti=n;i>=1; i--) {//from point I to n+1, everyone needs to get to S4 distance. +s4[i]=s4[i+1]+ (p[n+1].dis-p[i].dis) *P[i].num; - } $ $ll ans= (LL) inf* +; -ll s1=0, s2=0, s3=0; - for(intI=1; i<=n-2; i++) {//Violent enumeration of three point locations thes1+= num[i-1]*D[i]; -S2=0;Wuyi if(S1>=ans)Continue; the for(intj=i+1; j<=n-1; j + +){ -s2+= (num[j-1]-num[i]) *D[j]; Wus3=0; - if(S1+s2>=ans)Continue; About for(intk=j+1; k<=n;k++){ $s3+= (num[k-1]-NUM[J]) *D[k]; -ll s=s4[k+1];//represents the result of the last paragraph - if(ans>s1+s2+s3+s) { - //printf ("%d%d%d\n", i,j,k,n+1); output Zone Aans=s1+s2+s3+s; + } the } - } $ } theprintf"%i64d\n", ans); the } the the return 0; -}
View Code (for informational purposes only, happy to be good)
2015 the sixth annual Blue Bridge Cup C + + program design undergraduate B-Group finals-Residents ' meeting (programming big problem)