• 百练4091:最近餐馆


    传送门:http://bailian.openjudge.cn/practice/4091

    【题解】

    这垃圾题还卡常???

    首先我们发现暴力做是O(nqlogn)的,这个logn是log5000大概12

    我们发现可以把它优化到O(nqm)的,m=10,然后就过了。

    可能数据不满吧。。

    # 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:
    ", 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;
    }
    View Code
  • 相关阅读:
    时间复杂度和空间复杂度
    七、vue计算属性
    六、vue侦听属性
    四、vue派发更新
    五、vue nextTick
    三、vue依赖收集
    二、vue响应式对象
    递归
    链表
    TypeScript类型定义文件(*.d.ts)生成工具
  • 原文地址:https://www.cnblogs.com/galaxies/p/bailian4091.html
Copyright © 2020-2023  润新知