百练4091:最近餐馆
传送门:http://bailian.openjudge.cn/practice/4091
【题解】
这垃圾题还卡常???
首先我们发现暴力做是O(nqlogn)的,这个logn是log5000大概12
我们发现可以把它优化到O(nqm)的,m=10,然后就过了。
可能数据不满吧。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdac++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int M = 5000 + 10; const int mod = 1e9+7; # define RG register # define ST static int n, K, m; int chosen[M]; struct point { int a[5]; ld c; friend bool operator <(point a, point b) { return a.c<b.c; } }p[M], q; int main() { while(cin >> n >> K) { memset(chosen, 0, sizeof chosen); for (int i=1; i<=n; ++i) for (int j=0; j<K; ++j) scanf("%d", &p[i].a[j]); int Q; cin >> Q; for (int nQ = 1; nQ <= Q; ++nQ) { for (int i=0; i<K; ++i) scanf("%d", &q.a[i]); for (int i=1; i<=n; ++i) { p[i].c = 0.0; for (int j=0; j<K; ++j) p[i].c += (ld)(q.a[j] - p[i].a[j]) * (q.a[j] - p[i].a[j]); } scanf("%d", &m); printf("the closest %d points are:\n", m); for (int i=1; i<=m; ++i, puts("")) { ld cur = 1e18; int id; for (int j=1; j<=n; ++j) { if(chosen[j] == nQ) continue; if(p[j].c < cur) cur = p[j].c, id = j; } chosen[id] = nQ; for (int j=0; j<K; ++j) printf("%d ", p[id].a[j]); } } } return 0; }