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. 通过UDP建立TCP连接

    解释 通过UDP广播查询服务器的IP地址,然后再建立TCP点对点连接. 应用场景 在服务器IP未知时,并且已知服务器与客户端明确在一个局域网或者允许组播的子网下. 通过UDP发现服务器地址然后再进行T ...

  2. 6.10---mybatis中两张表查询数据dao层

  3. 给网站添加运行时间的JavaScript完整代码

    function secondToDate(second) { if (!second) { return 0; } var time = new Array(0, 0, 0, 0, 0); if ( ...

  4. 06--Qt窗口布局

    Qt窗口布局 标签: qtlayout 2012-05-05 07:56 3076人阅读 评论(0) 收藏 举报  分类: Qt开发(33)  版权声明:本文为博主原创文章,未经博主允许不得转载. 布 ...

  5. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  6. Centos7安装gitlab服务器

    1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...

  7. CAD插入非等比例的图块

    主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块.详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...

  8. Ansible 利用playbook批量部署mariadb

    环境说一下 192.168.30.21     ansible 192.168.30.25     client1 192.168.30.26     client2 这里我的ansible环境已经部 ...

  9. 默认ttl参考

    UNIX 及类 UNIX操作系统 ICMP 回显应答的 TTL 字段值为 255 Compaq Tru64 5.0 ICMP 回显应答的 TTL 字段值为 64 WINXP-32bit 回显应答的 T ...

  10. Whl自助搜索下载器

    本文转载自以下链接:https://github.com/Light-City/AutoDownloadWhl 源码地址: https://github.com/Light-City/AutoDown ...