hdu 4995 Revenge of kNN(暴力枚举)

Revenge of kNN

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 564    Accepted Submission(s): 134


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.
 

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
 

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.000000
Hint
For 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.
题意:在x轴上给你n个点的x坐标(乱序),同一个位置只能有一个点,每个点拥有一个价值,每次问你会给你一个i,把i点的价值改为这个点邻近的k个点的价值的平均值,如果左右有两个点距离相等取一开始给你的顺序比较靠前的那个,然后输出所有问你的点的总和

思路:很水的模拟题,但是题目太难懂了!!!!  以至于一开始以为是个难题,后来以为自己看懂了题目还是WA了几次,才发现没真正理解题意。。 

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 100010
int dis[N];
struct Node
{
    int x,id;
    double v;
} p[N];
bool cmp(Node a,Node b)
{
    return a.x<b.x;
}
int main()
{
    int T,t,id;
    int n,m,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&n,&m,&k);
        for(int i=1; i<=n; i++)
        {
            scanf("%d %lf",&p[i].x,&p[i].v);
            p[i].id=i;
        }
        double ans=0.0;
        sort(p+1,p+1+n,cmp);
        for(int i=1; i<=n; i++)
            dis[p[i].id]=i;
        while(m--)
        {
            scanf("%d",&t);
            int id=dis[t];
            double sum=0;
            int l=id-1,r=id+1;
            for(int i=1; i<=k; i++)
            {
                if(l>=1&&r<=n)
                {
                    if(p[id].x-p[l].x==p[r].x-p[id].x)
                    {
                        if(p[l].id<p[r].id)
                        {
                            sum+=p[l].v;
                            l--;
                        }
                        else
                        {
                            sum+=p[r].v;
                            r++;
                        }
                    }
                    else if(p[id].x-p[l].x<p[r].x-p[id].x)
                    {
                        sum+=p[l].v;
                        l--;
                    }
                    else if(p[id].x-p[l].x>p[r].x-p[id].x)
                    {
                        sum+=p[r].v;
                        r++;
                    }
                }
                else
                {
                    if(l<1&&r<=n)
                    {
                        sum+=p[r].v;
                        r++;
                    }
                    else if(l>=1&&r>n)
                    {
                        sum+=p[l].v;
                        l--;
                    }
                }
            }
            p[id].v=sum/k;
            ans+=p[id].v;
        }
        printf("%.6lf\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值