枚举,时间复杂度 O(L∗(m+n))
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<string>
#include<cstdio>
#include<utility>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int MAXN = 305,MAXM = 305, INF = 1<<30, Nya = -1;
int L ,m ,n ;
int p[MAXM] = {0};
int d[MAXN] = {0};
int ansp ,ansl = Nya;
//dist(i, j, S) = |Dj + S - Pi|
int main()
{
#ifndef ONLINE_JUDGE
freopen("sgu158.in","r",stdin);
freopen("sgu158.out","w",stdout);
#endif
std::cin >> L >> m;
L <<= 1;
for(int i = 1; i <= m ; i++)
{std::cin >> p[i]; p[i]<<=1;}
std::cin >> n; d[1] = 0;
for(int i = 2; i <= n ; i++)
{std::cin >> d[i]; d[i]<<=1;}
d[n+1] = INF;
for(int s = 0; s + d[n] <= L; s++)
{
int cal = 0;
for(int i = 1, j = 1; i <= m; i++)
{
while(d[j]+s <= p[i]) j++;
cal += std::min(abs(d[j] + s - p[i]),abs(d[j-1] + s - p[i]));
}
if(cal > ansl) ansp = s, ansl = cal;
}
std::cout << (ansp>>1);
if(ansp&1) std::cout << ".5";
std::cout << ' ';
std::cout << (ansl>>1);
if(ansl&1) std::cout << ".5";
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。