bc38 1002, bc39 1002

比赛的时候是对于每个数,记录下来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的更多相关文章
- myql 查询树形表结果:说说、说说的述评、评论的回复
myql 查询树形表结果:说说.说说的评论.评论的回复 有三张表关联表: 用户的说说表(ixt_customer_note) 说说的评论表(ixt_customer_note_comment) 评论的 ...
- awk之特征相同行的合并 ~转
awk之特征相同行的合并 文本: 1001 hisk01 1001 hisk02 1001 hisk03 1002 hisk04 1002 hisk05 1002 hisk06 1003 ...
- The first DP!
P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...
- ural 1251. Cemetery Manager
1251. Cemetery Manager Time limit: 1.0 secondMemory limit: 64 MB There is a tradition at the USU cha ...
- [CareerCup] 15.3 Renting Apartment III 租房之三
Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...
- [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 ...
- [CareerCup] 15.1 Renting Apartment 租房
Write a SQL query to get a list of tenants who are renting more than one apartment. -- TABLE Apartme ...
- CCF真题之数字排序
201503-2 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输入格式 输入的第一行包含一个整数n,表示给定数字的个数. 第二行包含n个整数,相邻的整数之间用一 ...
- CCF真题之图像旋转
201503-1 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...
随机推荐
- VC/MFC 使edit控件不能进行粘贴操作
这里使用消息拦截的方法 BOOL PersonDlg::PreTranslateMessage(MSG* pMsg) { if (GetDlgItem(IDC_EDIT_USER_ID)->m_ ...
- Haproxy+Keepalived搭建Weblogic高可用负载均衡集群
配置环境说明: KVM虚拟机配置 用途 数量 IP地址 机器名 虚拟IP地址 硬件 内存3G 系统盘20G cpu 4核 Haproxy keepalived 2台 192.168.1.10 192 ...
- J2SE学习小结
开始接触Java的学习,Java 2 Standard Edition为Java2平台的标准版,其包括了构成Java语言核心的类,此番学习算是学习了Java体系中的基础部分. 学习框架大致整理如下: ...
- 按 Eclipse 开发喜好重新布置 cocos2dx 目录层次
[tonyfield 2013.08.29 ] 1. Cocos2dx 库的改动 处于个人的固执,花一天时间重新布置了cocos2dx 2.1.4的目录层次,将android平台无关的代码全数裁剪, ...
- 怎么用CIFilter给图片加上各种各样的滤镜_1
近期在做对图像处理的app,而要对图像进行处理,CoreImage是离不开的框架. 上网Google了一下,大部分的博客都是仅仅讲了怎么给图像加上怀旧(CISepiaTone).模糊(CIBloom) ...
- 设计模式(4)-对象创建型模式-Prototype模式
1.对象创建型模式 1.4 Protoype模式 1.4.1需求 通过拷贝原形对象创建新的对象. 1.4.2结构 •P r o t o t y p e(Gr a p h i c) - ...
- [Android学习笔记]页面布局
线性布局:LinearLayout 1.集成ViewGroup,故可容纳多个View 2.线性布局,可设置水平或者垂直方向 相对布局:RelativeLayout
- mmap。
linux mmap 内存映射 mmap() vs read()/write()/lseek() 通过strace统计系统调用的时候,常常能够看到mmap()与mmap2().系统调用mmap()能够 ...
- vijos P1352 最大获利(最小割)
请不要随便指点别人该怎么做.每一个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在 ...
- Amazon.com : The Odyssey of the Manual Toothbrusher
Amazon.com : The Odyssey of the Manual Toothbrusher The Odyssey of the Manual Toothbrusher