HDU 4995 Revenge of kNN(暴力)

http://acm.hdu.edu.cn/showproblem.php?pid=4995


暴力,代码还没调对,这种题意不明的模拟题真的烦死了


#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000

using namespace std;

typedef long long ll;

typedef struct Node
{
	ll pos,id;
}Node;

double val[100000 + 20];

bool com(Node x, Node y)
{
	return x.pos < y.pos;
}

vector<Node>v;
map<ll, ll>ma;

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		v.clear();
		ma.clear();
		int n, m, k;
		scanf("%d %d %d", &n, &m, &k);
		for (int i = 0; i < n; i++)
		{
			ll x;
			double y;
			scanf("%lld %lf", &x, &y);
			Node T;
			T.pos = x; val[i] = y; T.id = i + 1;
			v.push_back(T);
		}
		sort(v.begin(), v.end(), com);
		for (int i = 0; i < n; i++)
		{
			ma[v[i].id] = i;
		}
		double ans = 0.0;
		for (int i = 0; i < m; i++)
		{
			ll q;
			ll pr, pl, pos;
			scanf("%lld", &q);
			pos = ma[q];
			pl = pos, pr = pos;
			double sum = 0.0;
			ll time = k;
			for (int j = 0; j < k;j++)
			{
				if ((pl - 1 >= 0) && (pr + 1 < n))
				{
					if (abs(v[pl - 1].pos - v[pos].pos) <= (abs(v[pr + 1].pos - v[pos].pos)))
						sum += val[--pl];
					else
						sum += val[++pr];
				}
				else if (pl - 1 >= 0)
					sum += val[--pl];
				else
					sum += val[++pr];
			}
			val[pos] = sum / k;
			ans += val[pos];
		}
		printf("%.6lf\n", ans);
	}
	return 0;
}


第二版 WA  待修改


#include<iostream>
#include<cstdio>
#include<set>
#include<string>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<utility>
#include<cmath>
#define mt(a) memset(a,0,sizeof (a))
#define fl(a,b,c) fill(a,b,c)
#define SWAP(a,b,t) (t=a,a=b,b=t)
#define inf 1000000

using namespace std;

typedef long long ll;

typedef struct Node
{
	ll pos, id;
	double val;
}Node;

bool com(Node x, Node y)
{
	return x.pos < y.pos;
}

vector<Node>v;
map<ll, ll>ma;

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		v.clear();
		ma.clear();
		int n, m, k;
		scanf("%d %d %d", &n, &m, &k);
		for (int i = 0; i < n; i++)
		{
			ll x, y;
			scanf("%lld %lld", &x, &y);
			Node T;
			T.pos = x; T.val = y; T.id = i + 1;
			v.push_back(T);
		}
		sort(v.begin(), v.end(), com);
		vector<Node>::iterator po1, po2;
		for (int i = 0; i < n; i++)
		{
			ma[v[i].id] = i;
		}
		double ans = 0.0;
		for (int i = 0; i < m; i++)
		{
			ll q;
			ll pr, pl, pos;
			scanf("%lld", &q);
			pos = ma[q];
			pl = pos, pr = pos;
			double sum = 0.0;
			ll time = k;
			for (int j = 0; j < k; j++)
			{
				if ((pl - 1 >= 0) && (pr + 1 < n))
				{
					if (abs(v[pl - 1].pos - v[pos].pos) > (abs(v[pr + 1].pos - v[pos].pos)))
					{
						sum += v[++pr].val;
					}
					else
					{
						sum += v[--pl].val;
					}
						
				}
				else if (pl - 1 >= 0)
				{
					
					sum += v[--pl].val;
				}
					
				else
				{
					sum += v[++pr].val;
				}
					
			}
			v[pos].val = sum / k;
			ans += v[pos].val;
		}
		printf("%.6lf\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值