题目链接: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. POJ1011 Sticks

    Description George took sticks of the same length and cut them randomly until all parts became at mo ...

  2. poj 1845 数论综合

    题意:求A^B的所有因数的和 mod 9901 sol:一开始毫无思路,因为很多定理都不知道-_-|| 1. 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^ ...

  3. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  4. mysql存储过程的学习

    平时在工作中写过很多存储过程,但有时候对某些存储过程还是有些困惑的,所以发一篇文章记录下. 标准存储过程写法 create procedure`myQueryTask`( IN Task_No VAR ...

  5. POJ 3273 Monthly Expense

    传送门 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John is an astounding accounting wiza ...

  6. APNs详细使用步骤

    1. 什么是推送通知 消息通知分本地通知和远程推送通知,是没有运行在前台的应用程序可以让它们的用户获得相关消息通知的方式.消息通知可能是一条消息,即将发生的日历事件,或远程服务器的新数据.当被操作系统 ...

  7. POJ 1789Truck History(pirme)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22648   Accepted: 8781 De ...

  8. leach和leach-c协议仿真

    http://blog.csdn.net/codingkid/article/details/7215216 1.复制leach_test为leach-c_test,修改里面的文件夹和输出文件名.并且 ...

  9. idea修改运行内存

    安装目录下的bin 找到idea.exe.vmoptions 最大的修改下-Xmx1024m 找到idea64.exe.vmoptions 最大的修改下-Xmx1024m

  10. 使用存取方法来设置Property value

    对比如下代码,第一种使用了存取方法来设置,第二种直接对实例变量操作.显然我们应该采用第一种, 使用第二种情况,简单的情况还好,如果情况一旦复杂,就非常容易出错.并且直接对实例变量操作,不会引发KVO通 ...