比赛的时候是对于每个数,记录下来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. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  2. Bootstrap表格的使用

    先定义前端table <table class="table table-striped table-bordered table-hover" id="expan ...

  3. 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)

    访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...

  4. MingW环境下的windows编程

    一般在进行windows编程时都使用vc++精简版,其插入菜单,图片等资源等更简单,且vc中对中文有更好的支持,win7下安装的Mingw中文并不能很好地显示,有光标显示的位置和光标实际位置不符的问题 ...

  5. ACM-最小生成树之畅通project——hdu1863

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  6. ThinkPHP框架配置自定义的模板变量(十)

    原文:ThinkPHP框架配置自定义的模板变量(十) 模板替换(手册有详细介绍对应的目录) __PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/ __ROOT__: 会替换成当 ...

  7. 调用ShellExecute需要头文件

    调用ShellExecute需要头文件 #include   "windows.h " #include   "shellapi.h "

  8. Java读取本地文件,并显示在JSP文件中

        当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相对路径,还可以是网络上的图片路径.在存 ...

  9. Linux 核心阅读工具vim+ctags+cscope+taglist

    今天.介绍vim+ctags+cscope+taglist的内核阅读配置. 当使用过之后,我相信大部分人都会舍弃之前的Eclipse(我就是活生生的一个样例).我们先来看看实现的界面是怎么样的: 我们 ...

  10. Android 调用谷歌语音识别

    調用谷歌语音识别其实很简单,直接利用 intent 跳转到手机里面的谷歌搜索 代码也很简单,直接调用方法 startVoiceRecognitionActivity() 如果大家手机里面没有谷歌搜索, ...