hdu 5021 Revenge of kNN II(树状数组,离散化,二分)

Revenge of kNN II

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 292    Accepted Submission(s): 91


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, again. 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.
(Have you ever tried the problem “Revenge of kNN”? They are twin problems!)
 

Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case begins with two integers N and M. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi and Ki follows, in which Ki indicating the number of k-Nearest Neighbors

[Technical Specification]
1. 1 <= T <= 5
2. 2 <= N <= 100 000
3. 1 <= M <= 100 000
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
7. 1 <= Ki <= N - 1
 

Output
For each test case, output sum of all queries rounded to three fractional digits.
 

Sample Input
  
  
1 5 3 1 2 2 3 3 6 4 8 5 8 2 2 3 2 4 2
 

Sample Output
  
  
17.000
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.
题意:跟 Revenge of kNN这个题一样。

思路:用二分取查找半径,然后树状数组求和即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100050
int dis[N];
double c[N];
struct Node
{
    int x,id;
    double v;
    bool operator < (const Node &a)const{
     return x<a.x;
    }
} p[N];
bool cmp(Node a,Node b)
{
    return a.x<b.x;
}
int finds_high(int x,int n)
{
    if(x>=p[n].x) return n;
    int l=1,r=n,ans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(p[mid].x>x) r=mid-1;
        else ans=mid,l=mid+1;
    }
    return ans;
}
int finds_down(int x,int n)
{
    if(x<=p[1].x) return 1;
    int l=1,r=n,ans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(p[mid].x>=x) ans=mid,r=mid-1;
        else l=mid+1;
    }
    return ans;
}
int lowbit(int x)
{
    return x&-x;
}
void add(int x,double v,int n)
{
    for(int i=x; i<=n; i+=lowbit(i))
        c[i]+=v;
}
double sum(int x)
{
    double ans=0;
    for(int i=x; i; i-=lowbit(i))
        ans+=c[i];
    return ans;
}
int main()
{
    int T,n,m;
    int q,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0; i<=100000; i++)
            c[i]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %lf",&p[i].x,&p[i].v);
            p[i].id=i;
        }
        sort(p+1,p+n+1,cmp);
        for(int i=1; i<=n; i++)
        {
            dis[p[i].id]=i;
            add(i,p[i].v,n);
        }
        double ans=0;
        while(m--)
        {
            scanf("%d %d",&q,&k);
            int id=dis[q];
            int d=p[id].x;
            int l=0,r=1000000000;
            double s=0;
            if(k>=n) ans=ans+(sum(n)-p[id].v)/(n-1);
            else
            {
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    /*int rr=finds_high(d+mid,n);
                    int ll=finds_down(d-mid,n);*/
                        Node t;
                        t.x=d+mid;
                        int rr=upper_bound(p+1,p+1+n,t)-p-1;
                        t.x=d-mid;
                        int ll=lower_bound(p+1,p+1+n,t)-p;
                    if(rr-ll==k+1&&p[rr].x-p[id].x==p[id].x-p[ll].x)
                    {
                        if(p[rr].id<p[ll].id) ll++;
                        else rr--;
                        s=s+sum(rr)-sum(ll-1)-p[id].v;
                        break;
                    }
                    else if(rr-ll==k)
                    {
                        s=s+sum(rr)-sum(ll-1)-p[id].v;
                        break;
                    }
                    else if(rr-ll<k) l=mid+1;
                    else if(rr-ll>k) r=mid-1;
                }
                add(id,s/k-p[id].v,n);
                p[id].v=s/k;
                ans+=p[id].v;
            }
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值