http://acm.hdu.edu.cn/showproblem.php?pid=4995
暴力,代码还没调对,这种题意不明的模拟题真的烦死了
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000
using namespace std;
typedef long long ll;
typedef struct Node
{
ll pos,id;
}Node;
double val[100000 + 20];
bool com(Node x, Node y)
{
return x.pos < y.pos;
}
vector<Node>v;
map<ll, ll>ma;
int main()
{
int T;
cin >> T;
while (T--)
{
v.clear();
ma.clear();
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; i++)
{
ll x;
double y;
scanf("%lld %lf", &x, &y);
Node T;
T.pos = x; val[i] = y; T.id = i + 1;
v.push_back(T);
}
sort(v.begin(), v.end(), com);
for (int i = 0; i < n; i++)
{
ma[v[i].id] = i;
}
double ans = 0.0;
for (int i = 0; i < m; i++)
{
ll q;
ll pr, pl, pos;
scanf("%lld", &q);
pos = ma[q];
pl = pos, pr = pos;
double sum = 0.0;
ll time = k;
for (int j = 0; j < k;j++)
{
if ((pl - 1 >= 0) && (pr + 1 < n))
{
if (abs(v[pl - 1].pos - v[pos].pos) <= (abs(v[pr + 1].pos - v[pos].pos)))
sum += val[--pl];
else
sum += val[++pr];
}
else if (pl - 1 >= 0)
sum += val[--pl];
else
sum += val[++pr];
}
val[pos] = sum / k;
ans += val[pos];
}
printf("%.6lf\n", ans);
}
return 0;
}
第二版 WA 待修改
#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000
using namespace std;
typedef long long ll;
typedef struct Node
{
ll pos, id;
double val;
}Node;
bool com(Node x, Node y)
{
return x.pos < y.pos;
}
vector<Node>v;
map<ll, ll>ma;
int main()
{
int T;
cin >> T;
while (T--)
{
v.clear();
ma.clear();
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; i++)
{
ll x, y;
scanf("%lld %lld", &x, &y);
Node T;
T.pos = x; T.val = y; T.id = i + 1;
v.push_back(T);
}
sort(v.begin(), v.end(), com);
vector<Node>::iterator po1, po2;
for (int i = 0; i < n; i++)
{
ma[v[i].id] = i;
}
double ans = 0.0;
for (int i = 0; i < m; i++)
{
ll q;
ll pr, pl, pos;
scanf("%lld", &q);
pos = ma[q];
pl = pos, pr = pos;
double sum = 0.0;
ll time = k;
for (int j = 0; j < k; j++)
{
if ((pl - 1 >= 0) && (pr + 1 < n))
{
if (abs(v[pl - 1].pos - v[pos].pos) > (abs(v[pr + 1].pos - v[pos].pos)))
{
sum += v[++pr].val;
}
else
{
sum += v[--pl].val;
}
}
else if (pl - 1 >= 0)
{
sum += v[--pl].val;
}
else
{
sum += v[++pr].val;
}
}
v[pos].val = sum / k;
ans += v[pos].val;
}
printf("%.6lf\n", ans);
}
return 0;
}