hdu 4995(枚举)
2016-06-20 19:47
417 查看
Revenge of kNN
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each test case, output sum of all queries rounded to six fractional digits.
[align=left]Sample Input[/align]
1
5 3 2
1 2
2 3
3 6
4 8
5 8
2
3
4
[align=left]Sample Output[/align]
17.000000
HintFor 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.解题思路:读懂题意,暴力枚举即可。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100005; struct Point { __int64 x; int id; double value; }p[maxn]; int n,m,k,cnt,idx[maxn]; bool cmp(Point a,Point b) { return a.x < b.x; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); for(int i = 1; i <= n; i++) { scanf("%I64d%lf",&p[i].x,&p[i].value); p[i].id = i; } sort(p+1,p+1+n,cmp); for(int i = 1; i <= n; i++) idx[p[i].id] = i; double sum = 0; while(m--) { int q; double ans = 0; scanf("%d",&q); int pos = idx[q]; int l = pos - 1, r = pos + 1; cnt = 0; while(cnt < k) { if(l >= 1 && r <= n) { if(p[pos].x - p[l].x < p[r].x - p[pos].x) { ans += p[l].value; l--; } else if(p[pos].x - p[l].x > p[r].x - p[pos].x) { ans += p[r].value; r++; } else if(p[l].id < p[r].id) { ans += p[l].value; l--; } else if(p[l].id > p[r].id) { ans += p[r].value; r++; } } else if(l == 0) { ans += p[r].value; r++; } else if(r == n + 1) { ans += p[l].value; l--; } cnt++; } p[pos].value = ans / k; sum += ans / k; } printf("%.6f\n",sum); } return 0; }
相关文章推荐
- C#编程中枚举类型的使用教程
- 枚举的用法详细总结
- PHP中Enum(枚举)用法实例详解
- c#入门之枚举和结构体使用详解(控制台接收字符串以相反的方向输出)
- 理解C#中的枚举(简明易懂)
- C#枚举中的位运算权限分配浅谈
- C++基础入门教程(四):枚举和指针
- FileShare枚举的使用小结(文件读写锁)
- Java枚举类用法实例
- 枚举窗口句柄后关闭所有窗口示例
- javascript模拟枚举的简单实例
- 深入剖析JavaScript中的枚举功能
- 枚举和宏的区别详细解析
- 深入理解C#中的枚举
- C#实现获取枚举中元素个数的方法
- 结合C++11的新特性来解析C++中的枚举与联合
- java中枚举的详细使用介绍
- java枚举的使用示例
- 详解Java中的迭代迭代器Iterator与枚举器Enumeration
- Java枚举详解及使用实例(涵盖了所有典型用法)