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度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...
随机推荐
- 配置QtCreator+CDB远程调试环境(要设置_NT_SYMBOL_PATH和QT_PLUGIN_PATH和Path)
相关环境信息:开发机Win7 x64.远程机器WinXP.调试器是CDB.Qt版本5.2.1 一.部署远程机器环境 我这里用的是虚拟机(Windows XP),根据你要调试的程序选择安装不同架构的Wi ...
- Xtrabackup使用指南 | 简单.生活
Xtrabackup使用指南 | 简单.生活 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好 ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- maven项目配置Project Facets时further configuration available不出来问题
如果下边的 further configuration available不出来 把Dynamic web module 去掉勾选,应用与项目,然后再点开项目的properties,再选中Dynami ...
- Mojo Mysql utf-8字符集 需要{mysql_enable_utf8 => 1}
get '/api/log_mon/get_log' => sub{ my $c = shift; my $env = $c->param('env'); my $host = $c-&g ...
- 使用CocoaPods出现 The `master` repo requires CocoaPods 0.32.1 - 问题解决
近期在使用CocoaPods为project配置第三方类库时出现了例如以下问题: [!] The `master` repo requires CocoaPods 0.32.1 - 明显是由于Coco ...
- Latin1的所有字符编码
ISO-8859-1 (ISO Latin 1) Character Encoding Contents The characters at a glance Character codes and ...
- Chrome App远程控制
現在google app連上chrome就能遠控了出了幾年了, 能用觸控控制mouse https://chrome.google.com/webstore/detail/chrome-remote- ...
- 依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦(转good)
依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦.所谓横切关注点,即影响应用多处的功能,这些功能各个应用模块都需要,但又不是其主要关注点,常见 ...
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验
在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...