hdu 4995(离散化下标+模拟)
Revenge of kNN
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 584 Accepted Submission(s): 136
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.
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
5 3 2
1 2
2 3
3 6
4 8
5 8
2
3
4
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
struct Node{
LL x;
int id1;
double v;
}node[N];
int idx[N];
int cmp(Node a,Node b){
return a.x<b.x;
}
int main()
{
int tcase,n,m,k,x;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++){
scanf("%lld%lf",&node[i].x,&node[i].v);
node[i].id1 = i;
}
sort(node+,node++n,cmp);
for(int i=;i<=n;i++){
idx[node[i].id1] = i;
}
double sum = ;
while(m--){
int id;
scanf("%d",&id);
int now = idx[id];
double avg = ;
int l = now-,r = now+;
for(int i=;i<=k;i++){
if(l>=&&r<=n){
LL dis1 = node[now].x - node[l].x;
LL dis2 = node[r].x - node[now].x;
if(dis1<dis2){
avg+=node[l--].v;
}else if(dis1>dis2){
avg+=node[r++].v;
}else { ///相等的话按照邻居原来的下标进行选择
int ori_l = node[l].id1;
int ori_r = node[r].id1;
if(ori_l<ori_r) {
avg+=node[l--].v;
}else{
avg+=node[r++].v;
}
}
}else if(l>=){
avg+=node[l--].v;
}else if(r<=n){
avg+=node[r++].v;
}
}
/* ///不知道WA的原因
int l=1,r=1;
for(int i=1;i<=k;i++){
if(now-l>=1&&now+r<=n){
LL dis1 = node[now].x - node[now-l].x;
LL dis2 = node[now+r].x - node[now].x;
if(dis1<dis2){
avg+=node[now-l].v;
l++;
}else if(dis1>dis2){
avg+=node[now+r].v;
r++;
}else { ///相等的话按照邻居原来的下标进行选择
int ori_l = node[now+l].id1;
int ori_r = node[now+r].id1;
if(ori_l<ori_r) {
avg+=node[now-l].v;
l++;
}else{
avg+=node[now+r].v;
r++;
}
}
}else if(now-l>=1){
avg+=node[now-l].v;
l++;
}else if(now+r<=n){
avg+=node[now+r].v;
r++;
}
}*/
node[now].v = avg/k;
sum+=avg/k;
}
printf("%.6lf\n",sum);
}
return ;
}
hdu 4995(离散化下标+模拟)的更多相关文章
- hdu 4995 离线处理+模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4995 给定一维坐标下的n个点,以及每个点的权值,有m次查询,每次将查询的x点上的权值修改为离x最近的k个点权值的 ...
- hdu 5139(离线处理+离散化下标)
Formula Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数
R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...
- POJ2729 Robocode(离散化与模拟-----提醒曾经爱玩游戏的自己没做出这个
题目链接 :http://poj.org/problem?id=2729 题目很长,有不少也是废话.类似小时候玩的坦克大战.每个坦克速度为10,炮弹速度为20.子弹出界就消失,坦克出不了界限.相向的子 ...
- HDU 5510---Bazinga(指针模拟)
题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...
- HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...
- HDU 2836 (离散化DP+区间优化)
Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...
- lines---hdu5124(离散化+数组模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 就是有n条在x轴的线段,给你线段的左右端点,每条线段都会覆盖点,求出最多被覆盖多少次: #inc ...
随机推荐
- Python之路-基础数据类型之字符串
字符串类型 字符串是不可变的数据类型 索引(下标) 我们在日常生活中会遇到很多类似的情况,例如吃饭排队叫号,在学校时会有学号,工作时会有工号,这些就是一种能保证唯一准确的手段,在计算机中也是一样,它就 ...
- POJ 2586 贪心+枚举
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15626 Accepted: 78 ...
- 动态规划:HDU1069-Monkey and Banana
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 3224: Tyvj 1728 普通平衡树(finger tree)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 19122 Solved: 8359[Submit][St ...
- 精简Docker镜像的五种通用方法
http://dockone.io/article/8163 精简Docker镜像的好处很多,不仅可以节省存储空间和带宽,还能减少安全隐患.优化镜像大小的手段多种多样,因服务所使用的基础开发语言不同而 ...
- 码农与UI沟通的日常
事情是这样的,这是一个兴趣群组的效果图. 我看了一眼没有帖子时的提示,觉得这样的提示 不走心 不能展现出我们团队对于人生及世界的深度理解和高尚的品格. 于是,我选择了表达内心的真实感受. 我觉得这完美 ...
- Ubuntu18.04安装docker、docker-compose、
Ubuntu18.04下Docker CE安装 Docker版本分为两个:Docker Community Edition (CE)和 Docker Enterprise Edition (EE).D ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- Leetcode 522.最长特殊序列II
最长特殊序列II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符实现,但 ...
- mojoportal中使用jquey的插件
以前在mojo中使用jquery的插件,都是把插件的文件内容直接写到了相关的模块中,这样的问题是不整洁,一大串代码. 如果直接在layout.master中引入插件文件,或者在自定义模块中引入插件文件 ...