the solution of CountNonDivisible by Codility
question:https://codility.com/programmers/lessons/9
To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn);
Then we iterate array A to get the ith non-divsors by A.size() - count(element) for element in divsor[A[i]] in divsors. Time complexity is O(n*?
); ?
represent the average of divsors
this method unsatisfy the time requirement , for two test case get TIMEOUT error. NEED IMPROVE IT LATER.
code:
#include <algorithm>
#include <map>
//this method not fast enough
vector<int> solution(vector<int> &A) {
// write your code in C++11
map<int,int> dic;
map<int,vector<int> > divsors;
int size = A.size();
int max = *max_element(A.begin(),A.end());
for(int i=0; i<size; i++){
dic[A[i]]++;
if(divsors.count(A[i])==0){
vector<int> vec(1,1);
divsors.insert(make_pair(A[i],vec));
}
} for(int i=2; i<= max; i++){
int element = i;
while(element <=max){
if(divsors.count(element)!=0 && find(divsors[element].begin(),divsors[element].end(),i)==divsors[element].end()){
divsors[element].push_back(i);
}
element+=i;
}
}
vector<int > res;
for(int i=0; i<size; i++){
vector<int> t = divsors[A[i]];
int cnt = size;
for(int j=0; j<t.size(); j++){
cnt -= dic[t[j]];
}
res.push_back(cnt);
}
return res;
}
the solution of CountNonDivisible by Codility的更多相关文章
- Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...
- Solution to Triangle by Codility
question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
随机推荐
- NHibernate学习(零)-本次学习遇到的错误汇总
问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...
- UIPickerView的应用
UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器.UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIC ...
- [ POI 2017 ] Podzielno
\(\\\) \(Description\) \(B\)进制数,每个数字\(i(i\in [0,B-1])\)有\(A_i\)个.用这些数字组成一个最大的\(B\)进制数\(X\)(不能有前导零,不需 ...
- Java—RequestMapping相关用法
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.它有6个属性:1.value:指定请求的具体地址:valu ...
- Android java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230
近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...
- Angular——事件指令
基本介绍 angular的事件指令都是ng-click,ng-blur....的形式,类似于js的事件 基本使用 <!DOCTYPE html> <html lang="e ...
- JS——null
变量被赋值为null,目的往往是为了销毁这个对象: var n1 = 1; n1 = null;
- java网络
title: java 网络 date: 2017年3月11日11:14:52 1. 复杂的东西就把他封装成对象 概述:(网络就是找到别人) 找到对方的机器,(找到对方的ip地址) 每个机器中有很多进 ...
- (转)分布式文件存储FastDFS(三)FastDFS配置
http://blog.csdn.net/xingjiarong/article/details/50559768 在上一节中我们一起搭建了一个单节点的FastDFS系统,但是仅仅将系统搭建起来是远远 ...
- Python 之有道翻译数据抓取
import requests import time def you_dao(): key = input("请输入要翻译的内容:") # key = "哈哈" ...