Codeforces Round #669 (Div. 2) ------ Big Vova

题目

在这里插入图片描述

题意

给定你一个数组a,你可以对数组a进行重新排序,然后得到新的数组b,对数组b进行gcd,得到数组c
,c[i] = gcd(b[1], b[2],b[i]),使得c数组是的字典序最大。求这样的数组b

题解

看数据范围之后,可以直接暴力。
b数组第一个数为a数组中最大的数,后面的数是能使得其gcd最大的数,依次往下就好

AC代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
const int N = 1e3 + 15;
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int T; cin >> T;
	while (T--) {
		int a[N], b[N];
		int n; cin >> n;
		int maxa = 0;
		int pt = -1;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			if (a[i] > maxa) {
				maxa = a[i];
				pt = i;
			}
		}
		int t = 0;
		int vis[N];
		b[t++] = maxa;
		memset(vis, 0, sizeof(vis));
		int maxgcd = maxa;
		vis[pt] = 1;
		for (int i = 0; i < n; i++) {
			int temp = 0, p;
			for (int j = 0; j < n; j++) {
				if (vis[j] == 0) {
					int x = gcd(maxgcd, a[j]);
					if (x > temp) {
						temp = x;
						p = j;
					}
				}
			}
			maxgcd = temp;
			b[t++] = a[p];
			vis[p] = 1;
		}
		for (int i = 0; i < n; i++)
			cout << b[i] << " ";
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值