CodeForces 582A【multiset使用样例】

题意:

给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数。

首先对数字进行排序。复杂度n*log(n^2)。

这题的证明有官方的英文题解==在这直接贴英文题解...

Let the answer be a1 ≤ a2 ≤ ... ≤ an. We will use the fact that gcd(ai, aj) ≤ amin(i, j).

It is true that gcd(an, an) = an ≥ ai ≥ gcd(ai, aj) for every 1 ≤ i, j ≤ n. That means that an is equal to maximum element in the table. Let set an to maximal element in the table and delete it from table elements set. We‘ve deleted gcd(an, an), so the set now contains all gcd(ai, aj), for every 1 ≤ i, j ≤ n and 1 ≤ min(i, j) ≤ n - 1.

By the last two inequalities gcd(ai, aj) ≤ amin(i, j) ≤ an - 1 = gcd(an - 1, an - 1). As soon as set contains gcd(an - 1, an - 1), the maximum element in current element set is equal to an - 1. As far as we already know an, let‘s delete the gcd(an - 1, an - 1), gcd(an - 1, an), gcd(an, an - 1)from the element set. Now set contains all the gcd(ai, aj), for every 1 ≤ i, j ≤ n and1 ≤ min(i, j) ≤ n - 2.

We‘re repeating that operation for every k from n - 2 to 1, setting ak to maximum element in the set and deleting the gcd(ak, ak), gcd(ai, ak), gcd(ak, ai) for every k < i ≤ n from the set.

One could prove correctness of this algorithm by mathematical induction. For performing deleting and getting maximum element operations one could use multiset or map structure, so solution has complexity .

来源:http://codeforces.com/blog/entry/20692

这里学了一下multiset的各种应用....

#include<stdio.h>
#include<queue>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
int n;
int ttmp[600];
multiset<int>s;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int n,tmp;
    scanf("%d",&n);
    for(int i=1;i<=n*n;i++)
    {
        scanf("%d",&tmp);
        s.insert(tmp);
    }
    int num=1;
    ttmp[num]=*s.rbegin();
    s.erase(s.find(*s.rbegin()));
    while(num<n)
    {
        num++;
        ttmp[num]=*s.rbegin();
        s.erase(s.find(*s.rbegin()));
        for(int i=num-1;i>=1;i--)
        {
            s.erase(s.find(gcd(ttmp[i],ttmp[num])));
            s.erase(s.find(gcd(ttmp[i],ttmp[num])));
        }
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d ",ttmp[i]);
    }
    return 0;
}
时间: 2024-08-23 10:55:02

CodeForces 582A【multiset使用样例】的相关文章

Python Socket 编程——聊天室演示样例程序

上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket 编程的理解. 聊天室程序需求 我们要实现的是简单的聊天室的样例,就是同意多个人同一时候一起聊天.每一个人发送的消息全部人都能接收到,类似于 QQ 群的功能,而不是点对点的 QQ 好友之间的聊天.例如以下图: 图来自:http://www.ibm.com/developerworks/linux/tu

ADF Faces导出Excel文件【附样例工程】

本文提供一个基于ADF Face组件开发样例工程,工程的实现过程分为3个部分以应对Excel导出开发中常见的处理. 1.空模版文件下载:将Excel文件视为普通文件提供下载操作. 2.数据文件输出,将数据内容输出为Excel文件,目标文件尽在服务端内存中存在,这种方式需要对Excel文件的内容处理,需要引入响应的类库. 3.模版文件填充数据后下载,基于服务端的物理文件为模板,将业务数据填入约定位置后提供下载,在实现方面需要为工作簿对象指定源文件输入流,并完成后续内容处理. 实现的基本思路,由AD

使用GPC分解多边形样例(Generic Polygon Clipper)

使用GPC分解多边形 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) GPC: Generic Polygon Clipper GPC支持分解多边形.多边形求差集.交集,异或.并集 GPC_DIFF,                         /* Difference                        */ GPC_INT,                          /* Intersection    

Apache Tomcat样例目录session操纵漏洞

0x00   背景 前段时间扫到的漏洞,研究了下,感觉挺有意思的,发出来和大家分享下,有啥不对的地方还请各位拍砖指正. Apache Tomcat默认安装包含"/examples"目录,里面存着众多的样例,其中session样例(/examples/servlets/servlet/SessionExample)允许用户对session进行操纵.因为session是全局通用的,所以用户可以通过操纵session获取管理员权限. 案例:http://www.wooyun.org/bugs

最大熵算法及简单样例

近期在学模式识别,正在看Introduction to Pattern Recognition这本书,挺不错的一本书.好.以下和大家一起来学习最大熵算法. 首先,最大熵算法是干什么的呢?通常是用来预计一个分布,至于把分布预计出来之后用来干什么,那要视详细问题而定. 那这里的"熵"是什么意思呢?它是指信息熵,一个分布的均匀程度能够用熵的大小来衡量.熵越大,就越均匀.而最大熵就是要求在满足特定约束下,分布是什么样的时候.熵最大.也就是越均匀越好. 为什么在满足特定约束下越均匀越好?由于你已

LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例

二次联通门 : LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例 /* LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例 很显然 贪心方程哦不 dp方程为 f[i][j]=f[i-1][j-k*k] 但是这样的话复杂度就是O(N ^ 5) 那么就用bitset优化一下 就ok了 */ #include <iostream> #include <cstdio> #include <bitset> void

Java向上转型和向下转型(附具体样例)

                                            Java向上转型和向下转型(附具体样例) 熬夜整理的关于Java向上和向下转型的样例,很的通俗易懂哦~~~~ 一.向上转型 package com.sheepmu; class Animal { public void eat() { System.out.println("父类的 eating..."); } } class Bird extends Animal { @Override publ

Relation Extraction中SVM分类样例unbalance data问题解决 -松弛变量与惩罚因子

转载自:http://blog.csdn.net/yangliuy/article/details/8152390 1.问题描述 做关系抽取就是要从产品评论中抽取出描述产品特征项的target短语以及修饰该target的opinion短语,在opinion mining里面属于很重要的task,很多DM.NLP相关的paper在做这方面的工作.基本的思路是: (1)从sentence的parse tree(比如stanford parser)中选取候选target结点和候选opinion结点,然

Python word_cloud 样例 标签云系列(三)

转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitHub 上面是官方样例.这一篇里的大部分尝试都基于这些样例进行修改.前提是你已经完成了安装,依照上一篇修改了 FONT_PATH . 还记得 http://zhuanlan.zhihu.com/666666/20432734 里提到的中文分词方法吧,这次我们就不再赘述对文本的预处理了.有所不同的是,在