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度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...
随机推荐
- Delphi -- 创建 桌面、发送到...、快速启动栏、开始菜单、程序菜单、右键菜 单
{================================================================= 功 能: 创建 桌面.发送到....快速启动栏.开始菜单.程序菜单 ...
- C++ Primer 学习笔记_76_模板和泛型编程 --模板定义[继续]
模板和泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- Swift - 自定义单元格实现微信聊天界面
1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...
- SVM入门(十)将SVM用于多类分类
源地址:http://www.blogjava.net/zhenandaci/archive/2009/03/26/262113.html 从 SVM的那几张图可以看出来,SVM是一种典型的两类分类器 ...
- Delphi的类型转换 good
Delphi是一种强类型转换的语言.在VC中,赋值符用″=″,例如x=1;到了Delphi赋值符就变成了″:=″,例如x:=1. 从赋值时用符号″:=″而不用″=″,就隐约可见Delphi对类型匹配要 ...
- c#Enum的用法
public enum ResType { Role = 0, Dept = 1, Group = 2, Site = 3, Org = 4, Sub=8 } 这里定义了一个enum ResTy ...
- Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot
Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, E ...
- 腾讯文学动作密集 疑为手Q发力移动阅读铺路
移动互联网的门票之争并未结束,百度收购91无线,阿里投资新浪微博.UC浏览器,网易推易信.云音乐等等,都是互联网巨头争夺移动互联网门票的最佳案例.不过,上述任何巨头都不可忽视腾讯这个“狠角色” ...
- Android获取设备採用的时间制式(12小时制式或24小时制式)
/** * 获取设备採用的时间制式(12小时制式或者24小时制式) * 注意: * 在模拟器上获取的时间制式为空 */ private void getTime_12_24(Context conte ...
- NET平台机器学习组件-Infer.NET
NET平台机器学习组件-Infer.NET(三) Learner API—数据映射与序列化 阅读目录 关于本文档的说明 1.基本介绍 2.标准数据格式的映射 3.本地数据格式映射 4.评估数据格式映射 ...