题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995
Revenge of kNN
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 372 Accepted Submission(s): 84
Problem Description
In pattern recognition, the k-Nearest Neighbors algorithm (or k-NN for short) is a non-parametric method used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space.
In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
---Wikipedia
Today, kNN takes revenge on you. You have to handle a kNN case in one-dimensional coordinate system. There are N points with a position Xi and value Vi. Then there are M kNN queries for point with index i, recalculate its value by averaging the values its k-Nearest Neighbors. Note you have to replace the value of i-th point with the new calculated value. And if there is a tie while choosing k-Nearest Neighbor, choose the one with the minimal index first.
In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
---Wikipedia
Today, kNN takes revenge on you. You have to handle a kNN case in one-dimensional coordinate system. There are N points with a position Xi and value Vi. Then there are M kNN queries for point with index i, recalculate its value by averaging the values its k-Nearest Neighbors. Note you have to replace the value of i-th point with the new calculated value. And if there is a tie while choosing k-Nearest Neighbor, choose the one with the minimal index first.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with three integers N, M and K, in which K indicating the number of k-Nearest Neighbors. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi follows.
[Technical Specification]
1. 1 <= T <= 5
2. 2<=N<= 100 000, 1<=M<=100 000
3. 1 <= K <= min(N – 1, 10)
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
Each test case begins with three integers N, M and K, in which K indicating the number of k-Nearest Neighbors. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi follows.
[Technical Specification]
1. 1 <= T <= 5
2. 2<=N<= 100 000, 1<=M<=100 000
3. 1 <= K <= min(N – 1, 10)
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
Output
For each test case, output sum of all queries rounded to six fractional digits.
Sample Input
1 5 3 2 1 2 2 3 3 6 4 8 5 8 2 3 4
Sample Output
17.000000HintFor the first query, the 2-NN for point 2 is point 1 and 3, so the new value is (2 + 6) / 2 = 4. For the second query, the 2-NN for point 3 is point 2 and 4, and the value of point 2 is changed to 4 by the last query, so the new value is (4 + 8) / 2 = 6. Huge input, faster I/O method is recommended.
Source
题意:输入n ,m, k 在一维坐标系中有n个点,有m次询问,每次询问一个qi(第qi次输入的点),然后把离这点距离最近的k个点的权值加和,同时将这个点的权值更新为权值和的1/k;
思路:直接模拟,但是有好几个要注意的地方,(1)要注意id与下标的关系,每次输入的点都有一个xi(坐标值),id(表示第几次输入的),然后按xi的大小从小到大排序,m次询问中询问的是id 而不是 排好序后的下标;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
const int N=1e5+100;
using namespace std;
double v[N];
int g[N][11],n,m,k;
struct node
{
int xi,id;
}a[N];
bool cmp(node a,node b)
{
return a.xi<b.xi;
}
bool judge(int x,int l,int r)
{
if(l<=0)return false;
if(r>n)return true;
if(a[x].xi-a[l].xi!=a[r].xi-a[x].xi)
return a[x].xi-a[l].xi<a[r].xi-a[x].xi;
return a[l].id<a[r].id;
}
void get_indx(int x)
{
int id=a[x].id;
int l=x-1,r=x+1;
for(int i=0;i<k;i++)
{
if(judge(x,l,r))
g[id][i]=a[l--].id;
else
g[id][i]=a[r++].id;
}
}
int main()
{
int T,qi;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%d%lf",&a[i].xi,&v[i]);
a[i].id=i;
}
sort(a+1,a+1+n,cmp);
//预处理
for(int i=1;i<=n;i++)
{
get_indx(i);
}
double total=0.0;
while(m--)
{
scanf("%d",&qi);
double sum=0.0;
for(int i=0;i<k;i++)
{
sum+=v[g[qi][i]];
}
v[qi]=sum/k;
total+=v[qi];
}
printf("%.6lf\n",total);
}
return 0;
}