题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995

题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出index Qi,求出离数组下标 Qi(从1开始) 最近的 K 个点,从新计算该下标所指示的value值,即等于 K 个 最近点的value值之和 / K,如果有多个K的最近点,那么选择坐标值靠前的那组。

模拟题。首先对将位置(因为输入不一定按顺序从左至右递增排序,样例有骗人的嫌疑)从小到大排序,然后求出每一个点的 K 个最近点,最近是通过xi 离当前 xpos 的距离来判断的,不断试探。如果发现左边的点的xl 比 右边的的点 xr 离 xi的距离近,那么就选左边的点,所以要通过进一步排序比较,Judge()函数中return point[pos].xi - point[l].xi < point[r].xi - point[pos].xi; 就是通过比较两边距离值来决定是选左边的那个点还是右边的那个点。(处理得非常地巧妙)

这个代码是学人家的,自以为看懂,写起来还是反反复复修改了很多次才成功~~~

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; typedef __int64 LL;
const int maxn = 1e5 + ;
double vi[maxn];
vector<int> g[maxn];
int N, M, K; struct node
{
int id, xi;
}point[maxn]; int cmp(const node& a, const node& b)
{
return a.xi < b.xi;
} bool Judge(int pos, int l, int r)
{
if (r > N)
return true;
if (l <= )
return false;
if (point[pos].xi - point[l].xi != point[r].xi - point[pos].xi) // 离两边距离值不同,选择距离值更小的那个点
return point[pos].xi - point[l].xi < point[r].xi - point[pos].xi;
return point[l].id < point[r].id; // 如果离两边的距离值相同,那么选择坐标值小的那个
} void Get_KNN(int pos) // 获得每个点的最近 k 个点是那些
{
int id = point[pos].id;
int l = pos - , r = pos + ;
for (int i = ; i < K; i++)
{
if (Judge(pos, l, r))
g[id].push_back(point[l--].id);
else
g[id].push_back(point[r++].id);
}
} int main()
{
int T;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%d%d%d", &N, &M, &K);
for (int i = ; i <= N; i++)
{
scanf("%d%lf", &point[i].xi, &vi[i]);
point[i].id = i;
}
sort(point+, point++N, cmp);
for (int i = ; i <= N; i++)
Get_KNN(i);
int ask;
double ans = ;
for (int i = ; i < M; i++)
{
scanf("%d", &ask);
double sum = ;
for (int j = ; j < g[ask].size(); j++)
sum += vi[g[ask][j]];
vi[ask] = sum / K;
ans += vi[ask];
}
printf("%.6lf\n", ans);
for (int j = ; j <= N; j++)
g[j].clear();
}
}
return ;
}

BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告的更多相关文章

  1. BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...

  2. BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  3. BestCoder8 1002 Revenge of Nim(hdu 4994) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994 题目意思:有 n 个 heap(假设从左至右编号为1-n),每个 heap 上有一些 objec ...

  4. BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

  5. BestCoder11(Div2) 1003 Boring count (hdu 5056) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 题目意思:给出一条只有小写字母组成的序列S,问当中可以组成多少条每个字母出现的次数 <= ...

  6. BestCoder18 1002.Math Problem(hdu 5105) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...

  7. BestCoder17 1002.Select(hdu 5101) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

  8. BestCoder8 1001.Summary(hdu 4989) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...

  9. BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...

随机推荐

  1. 【UVA 1451】Average

    题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...

  2. rpm常用选项

    httpd-2.2.15-39.el6.centos.x86_64.rpmhttpd   -      2.2.15-    39.el6.centos.       x86_64 .rpm软件名称- ...

  3. Cocos2d-X3.0 刨根问底(二)----- 从HelloWorld开始

    小鱼习惯直接从代码实例来学习一套成型的引擎库. 运行cpp-empty-test 一个典型的HelloWorld程序翻看代码结构 看到了 main.h与main.cpp文件就从这里开始 #ifndef ...

  4. Azure怎么使用ftp登录

    1.下载配置文件 2.拷贝FTP的地址 3.查看配置文件里面的用户名和密码 4.登录

  5. ethtool使用记录

    网卡出现很诡异的问题,把电脑连到一些交换机上是工作的,连到另外一些就不行...交换机上的link灯还时不时的闪一下,看起来像是在尝试连接. 用dmesg查看,看到下面的信息: [ 1112.92211 ...

  6. Opencv加载和显示图片

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

  7. Linux mount/unmount命令(转)

    格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有:-a 安装在/etc/fstab文件中类出的所有文件系统.-f 伪装mount,作出检查设备和目录的样子,但并不真正挂载文件系 ...

  8. #error和#line实例

    1.#include <stdio.h>#define CONST_NAME1 "CONST_NAME1"#define CONST_NAME2 "CONST ...

  9. c++ std::string.c_str()

    语法: const char *c_str();搜索 c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...

  10. CSS3 动画animation

    关键帧 什么是关键帧.一如上面对Flash原理的描述一样,我们知道动画其实由许多静态画面组成,第一个这样的静态画面可以表述为一帧.其中关键帧是在动画过程中体现了物理明显变化的那些帧. 比如之前的例子中 ...