Revenge of kNN
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 296 Accepted Submission(s): 66
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
比赛时太水,把
queried 的 Qi看成xi了,wa到死。。
然后比赛后改了还是wa。。
原来判断相等的时候是按Qi小的来。。我还没改过来。。
顺便说一下,以后p--写的时候小心点,容易错。。
读题能力有待加强。。
/***********************************************\
|Author: YMC
|Created Time: 2014/9/12 19:39:45
|File Name: c.cpp
|Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;
using namespace std;
struct st{
int id;
int x;
double v;
int l,r;
}da[100005];
int n,m,k;
bool cmp(st a,st b){
return a.x < b.x;
}
int u;
map<int,int> ma;
int main() {
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
#endif
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
ma.clear();
rep(i,n){
scanf("%d%lf",&da[i].x,&da[i].v);
da[i].id = i+1;
}
sort(da,da+n,cmp);
rep(i,n){
ma[da[i].id] = i;
}
da[0].l = 0;
da[0].r = 0+k;
for(int i=1;i<n-1;++i){
int lx = i-1;
int rx = i+1;
int p = k;
while(p-- > 0){
if(da[i].x - da[lx].x == da[rx].x - da[i].x){
if(da[lx].id < da[rx].id){
lx --;
if(lx == -1) while(p-- > 0){
rx ++;
}
} else {
rx ++;
if(rx == n){
while(p-- > 0){
lx --;
}
}
}
continue; //important!
}
if(da[i].x - da[lx].x < da[rx].x - da[i].x){
lx --;
if(lx == -1) while(p-- > 0){
rx ++;
}
}else {
rx ++;
if(rx == n){
while(p-- > 0){
lx --;
}
}
}
}
da[i].l = lx+1;da[i].r = rx-1;
}
da[n-1].l = n-1-k;
da[n-1].r = n-1;
//rep(i,n) cout<<i<<" "<<da[i].l<<" "<<da[i].r<<endl;
double sum = 0;
while(m--){
scanf("%d",&u);
int tt = ma[u];
double su = 0;
int l = da[tt].l,r = da[tt].r;
for(int i = l;i<=r;++i) su += da[i].v;
su -= da[tt].v;
da[tt].v = su / k;
sum += da[tt].v;
}
printf("%.6lf\n",sum);
}
return 0;
}