zoukankan      html  css  js  c++  java
  • Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大,

    首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序。

    经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i]之一。

    div2路漫漫。。。

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef int ll;
    
    ll a[501];
    int g[501*501];
    ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; }
    map<int,int> mp;
    
    //#define LOCAL
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif
        int n; scanf("%d",&n);
        int sz = n*n;
        for(int i = 0; i < sz; i++){
            scanf("%d",g+i);
        }
        sort(g,g+sz,greater<int>());
    
        int c = 0,j = 0;
        a[c++] = g[j++];
        for(int i = 1; i < n; i++){
            while(j<sz && mp[g[j]]){
                mp[g[j]]--;
                j++;
            }
            if(j == sz) break;
            for(int k = c-1; k >= 0; k--){
                mp[gcd(a[k],g[j])] += 2;
            }
            a[c++] = g[j++];
        }
        while(c--){
            printf("%d",a[c]);
            if(c) putchar(' ');
        }
        return 0;
    }
  • 相关阅读:
    CentOS7安装node环境
    【异常】update更新java.sql.SQLException: Duplicate entry '2019-07-30 00:00:00-110100' for key
    MySQL查询多行重复数据SQL
    Phoenix批量提交优化,官网的demo
    (转) 读懂IL
    Docker
    Docker
    Rest之路
    (转)Docker
    Docker
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4854787.html
Copyright © 2011-2022 走看看