题目链接: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. Dinic问题

    问题:As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offic ...

  2. --hdu 1114 Piggy-Bank(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 AC code: #include<bits/stdc++.h> using nam ...

  3. 锋利的jQuery-4--停止动画和判断是否处于动画状态(防止动画加入队列过多的办法)

    1.停止元素的动画:stop([cleanQueue, gotoEnd]):第一个参数代表是否要清空未执行完的动画队列,第二个参数代表是否直接将正在执行的动画跳转到末状态. 无参数stop():立即停 ...

  4. BurpSuite实例教程

    很久以前就看到了Burp suite这个工具了,当时感觉好NB,但全英文的用起来很是蛋疼,网上也没找到什么教程,就把这事给忘了.今天准备开始好好学习这个渗透神器,也正好给大家分享下.(注:内容大部分是 ...

  5. Iterator&Vector应用实例

    public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-gene ...

  6. Visual Studio Online Integrations-Other

                                                         原文:yuanhttp://www.visualstudio.com/zh-cn/explor ...

  7. C++ 中宏的使用 --来自:http://blog.csdn.net/hgl868/article/details/7058906

    宏在代码中的使用实例: g_RunLog2("Middleware client for Linux, build:%s %s", __DATE__, __TIME__); 下面详 ...

  8. CSS3 transforms 3D翻开

    R T L B   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  9. 使用node.js制作简易爬虫

    最近看了些node.js方面的知识,就像拿它来做些什么.因为自己喜欢摄影,经常上蜂鸟网,所以寻思了一下,干脆做个简单的爬虫来扒论坛的帖子. 直接上代码吧. var sys = require(&quo ...

  10. HTML 标准属性 和 事件属性

    HTML的公共属性 HTML 和 XHTML 标签支持的标准属性 核心属性 (Core Attributes) 以下标签不提供下面的属性:base.head.html.meta.param.scrip ...