Codeforces 583 DIV2 GCD Table 贪心
原题链接:http://codeforces.com/problemset/problem/583/C
题意:
大概就是给你个gcd表,让你还原整个序列。
题解:
由$GCD(a,a)=a$,我们知道最大的那个数一定是原序列中的数,然后每次从集合中选取最大的数出来,和已经构造好的序列进行gcd,删除gcd出来的值即可。
代码:
#include<iostream>
#include<cstring>
#include<map>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
using namespace std; int gcd(int a, int b){
return b==?a:gcd(b,a%b);
} map<int,int> ma;
vector<int> ans; int n; int main() {
cin.sync_with_stdio(false);
cin >> n;
for (int i = ; i < n * n; i++) {
int x;
cin >> x;
ma[-x]++;
} if (n == ) {
cout << -ma.begin()->first << endl;
return ;
}
auto it=ma.begin();
while(it!=ma.end()) {
while (it != ma.end() && it->second == )it++;
if(it==ma.end())break;
it->second--;
for (auto c:ans)ma[-gcd(c, -it->first)] -= ;
ans.push_back(-it->first);
}
for (auto c:ans)
cout << c << " ";
cout << endl;
return ;
}
Codeforces 583 DIV2 GCD Table 贪心的更多相关文章
- Codeforces 583 DIV2 Robot's Task 贪心
原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使 ...
- Codeforces 338 D. GCD Table
http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...
- Codeforces 583 DIV2 Asphalting Roads 模拟
原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...
- Codeforces H. Maximal GCD(贪心)
题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【Codeforces 582A】 GCD Table
[题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数. G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...
- 【Codeforces 582A】GCD Table
[链接] 我是链接,点我呀:) [题意] 给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组 让你求出原数组A [题解] 我们假设原数组是A 然后让A数组满足A ...
- Codeforces Round #323 (Div. 2) C. GCD Table 暴力
C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...
- Codeforces Round #323 (Div. 2) C. GCD Table map
题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...
- Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心
C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- sql:where中多种状况简便写法
字段名:Bran,block,are ,store 四个字段中存在值等于0或者不等于0,两种情况.where中如果用if等条件判断会有16中组合,如果采用where中的条件就避免了这个情况. decl ...
- P3386 【模板】二分图匹配(匈牙利&最大流)
P3386 [模板]二分图匹配 题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正 ...
- HDU1042 A * B Problem Plus
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Go语言之反射(三)
结构体转JSON JSON格式是一种用途广泛的对象文本格式.在Go语言中,结构体可以通过系统提供的json.Marshal()函数进行序列化.为了演示怎么样通过反射获取结构体成员以及各种值的过程,下面 ...
- github readme.md 添加图片
简要: 将图片放在仓库里面,在文件里链接它,最后 push 到 github 上. github 图片链接格式: (http://github.com/yourname/your-repository ...
- jenkins忘记管理员登陆密码
配置文件的路径在.../jenkins/config.xml (线上路径是/usr/local/tomcat7/webapps/jenkins/config.xml) 修复办法:千万注意:修复前一定要 ...
- 并发编程——IO模型(6)
1.IO模型分类 同步IO #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不会返回.按照这个定义,其实绝大多数函数都是同步调用.但是一般而言,我们在说同步.异步的时候,特指那些需要 ...
- hnust 搬书
问题 G: 搬书 时间限制: 1 Sec 内存限制: 128 MB提交: 576 解决: 49[提交][状态][讨论版] 题目描述 XCQ队长要退役啦,由于队长常年刷题,机位上摆着各类算法书,一个 ...
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- HTML5与HTML4的比较
HHTML5封装一些标签和属性,方便了开发. <form> <p> <label>Username:<input name="search" ...