题目链接:hdu 4995 Revenge of kNN
题目大意:给定一维坐标下的n个点,以及每个点的权值,有m次查询,每次将查询的x点上的权值修改为离x最近的k个点权值的平均和,有相同取序号小的。最后输出修改值的总和。
解题思路:模拟一下就行了,注意下标关系。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+5;
struct point {
int x, id;
}p[maxn];
int N, M, K, g[maxn][11];
double v[maxn];
bool cmp (const point& a, const point& b) {
return a.x < b.x;
}
bool judge (int i, int l, int r) {
if (r > N)
return true;
if (l <= 0)
return false;
if (p[i].x - p[l].x != p[r].x - p[i].x)
return p[i].x - p[l].x < p[r].x - p[i].x;
return p[l].id < p[r].id;
}
void get_KNN(int x) {
int id = p[x].id;
int l = x - 1, r = x + 1;
for (int i = 0; i < K; i++) {
if (judge(x, l, r))
g[id][i] = p[l--].id;
else
g[id][i] = p[r++].id;
}
}
void init () {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
scanf("%d%lf", &p[i].x, &v[i]);
p[i].id = i;
}
sort(p + 1, p + N + 1, cmp);
for (int i = 1; i <= N; i++)
get_KNN(i);
}
int main () {
int cas, x;
scanf("%d", &cas);
while (cas--) {
init();
double ans = 0;
for (int i = 0; i < M; i++) {
scanf("%d", &x);
double tmp = 0;
for (int j = 0; j < K; j++)
tmp += v[g[x][j]];
v[x] = tmp / K;
ans += v[x];
}
printf("%.6lf\n", ans);
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
hdu动态规划算法集锦
自己做的HDU ACM已经AC的题目
hdu题目分类
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
HDU图论题目分类
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码