比赛的时候是对于每个数,记录下来a[i], 并记录该树的下标hash[a[i]]

然后枚举a[i]的倍数,如果a[i]的倍数存在(设为k*a[i]),那么vis[k*a[i]]是不为0的

那么可以这样枚举得到最小的下标,但是比赛的时候不懂算时间复杂度,就随便提交了一下,没想到过了。

后来看了下题解,原来时间复杂度是这样算的

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
const int N = + ;
int vis[N];
int a[N];
int main()
{
int n, i, ans,j;
while (scanf("%d", &n) != EOF)
{
memset(vis, , sizeof(vis));
for (i = ; i <= n; ++i)
{
scanf("%d", &a[i]);
vis[a[i]] = i;
}
ans = ;
for (i = ; i <= n; ++i)
{
bool find = false;
int index;
for (j = ; j*a[i] <= ; ++j)
{
int v = j * a[i];
//找到最小的下标
if (!vis[v])
continue;
if (vis[v] < i)
continue;
if (!find)
{
index = vis[v];
find = true;
}
else
index = min(index, vis[v]); }
if (find)
ans += index;
}
printf("%d\n", ans);
}
return ;
}

然后想起bc38场的第2题好像也是类似这样子。

我可以hash每个数,即hash[a[i]]++

然后从大到小枚举约数,然后再枚举约数的倍数,如果出现过两次约数的倍数,那么该约数就是最大的约数。 需要注意的是因为a[i]可能重复,所以hash[a[i]]++

这题的时间复杂度和上面一样,也是O(nlgn)

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
const int N = + ;
int a[N];
int vis[N];
int main()
{
int t, n, i, k;
int ans,Max;
scanf("%d", &t);
for (k = ; k <= t; ++k)
{
Max = -;
scanf("%d", &n);
memset(vis, , sizeof(vis));
for (i = ; i < n; ++i)
{
scanf("%d", &a[i]);
Max = max(Max, a[i]);
vis[a[i]] ++;
}
for (i = Max; i >= ; --i)//枚举约数
{
int flag = ;
for (int j = ; j*i <= Max; ++j)//枚举约数的倍数,
{
int v = j * i;
flag += vis[v];
if (flag >=)
break;
}
if (flag >= )
break;
}
printf("Case #%d: %d\n", k, i);
}
return ;
}

bc38 1002, bc39 1002的更多相关文章

  1. myql 查询树形表结果:说说、说说的述评、评论的回复

    myql 查询树形表结果:说说.说说的评论.评论的回复 有三张表关联表: 用户的说说表(ixt_customer_note) 说说的评论表(ixt_customer_note_comment) 评论的 ...

  2. awk之特征相同行的合并 ~转

    awk之特征相同行的合并 文本: 1001  hisk01 1001  hisk02 1001  hisk03 1002  hisk04 1002  hisk05 1002  hisk06 1003 ...

  3. The first DP!

    P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...

  4. ural 1251. Cemetery Manager

    1251. Cemetery Manager Time limit: 1.0 secondMemory limit: 64 MB There is a tradition at the USU cha ...

  5. [CareerCup] 15.3 Renting Apartment III 租房之三

    Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...

  6. [CareerCup] 15.2 Renting Apartment II 租房之二

    Write a SQL query to get a list of all buildings and the number of open requests (Requests in which ...

  7. [CareerCup] 15.1 Renting Apartment 租房

    Write a SQL query to get a list of tenants who are renting more than one apartment. -- TABLE Apartme ...

  8. CCF真题之数字排序

    201503-2 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输入格式 输入的第一行包含一个整数n,表示给定数字的个数. 第二行包含n个整数,相邻的整数之间用一 ...

  9. CCF真题之图像旋转

    201503-1 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...

随机推荐

  1. mysql union ,UNION RESULT

    mysql> explain select * from t100 union all select * from t200; +----+--------------+------------ ...

  2. 浅析Delphi Container库(有开源的DCLX)

    与Java和C++相比,Delphi对容器的支持实在少得可怜.Java有强大的集合框架,C++更有STL,Delphi有什么呢,不就是TList几个小巧的列表类,而TCollection系列的类更多只 ...

  3. android在Canvas使用drawBitmap画一幅画

    1.画图的主要方法 //Bitmap:图片对象,left:向左偏移.top: 顶部偏移     drawBitmap(Bitmap bitmap, float left, float top, Pai ...

  4. Python使用cx_Oracle模块连接操作Oracle数据库

    1. 简单介绍 cx_Oracle 是一个用来连接并操作 Oracle 数据库的 Python 扩展模块, 支持包含 Oracle 9.2 10.2 以及 11.1 等版本号 2.安装 最好是去官网h ...

  5. 基于模糊Choquet积分的目标检测算法

    本文根据论文:Fuzzy Integral for Moving Object Detection-FUZZ-IEEE_2008的内容及自己的理解而成,如果想了解更多细节,请参考原文.在背景建模中,我 ...

  6. opencv 训练自己的分类器汇总

    原地址:http://www.cnblogs.com/zengqs/archive/2009/02/12/1389208.html OpenCV训练分类器 OpenCV训练分类器 一.简介 目标检测方 ...

  7. 浅谈spring——spring MVC(十一)

    springMVC框架主要是围绕DispatcherServlet这个核心展开,它负责拦截请求并将其分派给相应的的处理器处理,然后将结果响应给用户.包括注解驱动控制器.请求及响应信息处理.视图解析.本 ...

  8. RANSAC - 随机采样一致性算法

    RANSAC范例的正式描述如下: 首先,要给定: 1一个模型,该模型需要最少n个数据点去实例化它的自由参数: 2一组数据点P,P中包含数据点的数量#(P)大于n. 然后, 从P中随机地选择n个点(组成 ...

  9. 方案猿身高project联赛,艺术家,相反,养殖场!-------三笔

    已经看到了程序猿在电影中都是非常厉害的人物,硬道理键盘噼里啪啦后,奇妙的事情会发生. 当我报了这个专业,開始认真的写程序,在这个领域学习的时候,却发现非常多干这一行 的都自称"码农" ...

  10. 断言Assert的使用

     转载地址:http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数,事实上,它居然是个宏 ...