-
Notifications
You must be signed in to change notification settings - Fork 4
/
158_2.cpp
69 lines (62 loc) · 1.55 KB
/
158_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define rrep(i,j,k) for (int i=j;i>=k;--i)
using namespace std;
const int INF = 0x7FFFFFFF/2;
int M,N,ned[333];
double maxSum,maxPos,D[333],P[333],L;
void theIniter()
{
cin >> L >> M;
rep(i,1,M) cin >> P[i];cin >> N;
rep(i,2,N) cin >> D[i];
rep(i,1,M)
{
double mini = INF;
rep(j,1,N) if (fabs(P[i]-D[j])<mini){mini=fabs(P[i]-D[j]),ned[i]=j;}
}maxSum = maxPos = 0;
rep(i,1,M) maxSum+=fabs(P[i]-D[ned[i]]);
}
void theSolver()
{
rep(i,1,2*L)
{
double k = i*0.5;
if (k+D[N]>L) break;
rep(j,1,M)
{
int l,r,mid;
mid = ned[j];l=ned[j]-1;r = ned[j]+1;
while (l<=0) l++;while (r>=N) r--;
double tmp = INF;int pos;
if (fabs(D[mid]+k-P[j])<tmp)tmp=fabs(D[mid]+k-P[j]),pos=mid;
if (fabs(D[l]+k-P[j])<tmp) tmp=fabs(D[l]+k-P[j]),pos=l;
if (fabs(D[r]+k-P[j])<tmp) tmp=fabs(D[r]+k-P[j]),pos=r;
ned[j] = pos;
}
double tmp = 0;
rep(j,1,M) tmp+=fabs(D[ned[j]]+k-P[j]);
if (tmp>maxSum) maxSum = tmp,maxPos = k;
}
}
void thePrinter()
{
cout << setiosflags(ios::fixed) << setprecision(1) << maxPos << " " << maxSum << endl;
}
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
theIniter();
theSolver();
thePrinter();
return 0;
}