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的更多相关文章

  1. Solution of NumberOfDiscIntersections by Codility

    question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...

  2. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  3. 做了codility网站上一题:CountBoundedSlices

    在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...

  4. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  5. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  6. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  7. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  8. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  9. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

随机推荐

  1. 关于static函数在类中的定义和使用

    刷题的时候遇到了这样一个问题:平时经常使用 sort()函数, 对结构体进行排序, 但是在类中使用时会出现 这样的错误提示:“Solution::cmp”: 函数调用缺少参数列表:请使用“&S ...

  2. A* 寻路算法[转载]

    A* 寻路算法 转载地址:http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html 原文地址: http://www.gamedev ...

  3. Java. How to use headless browsers for crawling web and scraping data from website.--转

    https://www.linkedin.com/pulse/java-how-use-headless-browsers-crawling-web-scraping-data-taluyev/ Di ...

  4. Linux 查询PID和端口号

    https://www.cnblogs.com/understander/p/5546458.html

  5. HDU_1085_Holding Bin-Laden Captive!_母函数

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  6. 非常好用的1款UI自动化测试工具:airTest

    网易团队开发的UI自动化测试神器airTest,下载地址:http://airtest.netease.com/tutorial/Tutorial.html Appium和airTest对比,我的看法 ...

  7. CAD当前选择实体发生变化调用事件(com接口)

    主要用到函数说明: _DMxDrawXEvents::SelectModified 当前选择实体发生变化,会调用该事件,详细说明如下: 参数 说明 IDispatch* pAryId 当前被选择的实体 ...

  8. Django - ORM实现用户登陆

    1.路由分发cmdb(app)下urls.py中,建立url与函数对应关系 2.login.html代码: 3.views.py中,login函数,确认是否登陆成功 备注:从前端 获取用户名,密码,在 ...

  9. Async/await语法糖实现(Generator)

    // generator也是一种迭代器(Iterator) 有next方法,并返回一个对象{value:...,done:...} function run(generatorFunction) { ...

  10. How To:使用dmidecode获取机器序列号(Serial number)

    使用dmidecode可以获取关于机器的诸多信息,比如机器的序列号   [root@dc1db01 ~]# dmidecode -s system-serial-number   processor的 ...