cf 168 k-Multiple Free Set bin_search

简介:


     一开始写的是逆序的,WA了,改成正序AC,想来没有道理,细看后才发现是改了当前值,让二分序列不再有序……o(╯□╰)o,正序只能是侥幸罢了


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
int q[100010];
int abs(int a){return a>0?a:-a;}
int bin(int l,int r,long long x)
{
    int mid;
    if(x>abs(q[r]))return -1;
    while(l<r)
    {
        mid=l+((r-l)>>1);
        if(abs(q[mid])<x)l=mid+1;
        else r=mid;
    }
    if(q[l]==x)return l;
    else return -1;
}

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int i;
    for(i=0;i<n;i++)
        scanf("%d",&q[i]);
    sort(q,q+n);
    int ans=0;
    for(i=n-1;i>=0;i--)
    {
        if(q[i]<0)continue;
        ans++;
        if(q[i]%k==0)
        {
            r=bin(0,i-1,q[i]/k);
            if(r!=-1) q[r]=-q[r];//一开始这是-1
        }
    }
    printf("%d\n",ans);
}


目录
相关文章
|
1月前
|
XML C# 数据格式
The data at the root level is invalid. Line 1, position 1.
The data at the root level is invalid. Line 1, position 1.
44 0
|
1月前
|
存储
tracker_query_storage fail, error no: 28, error info: No space left on device
tracker_query_storage fail, error no: 28, error info: No space left on device
56 0
|
Unix Linux 异构计算
成功解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See /var/log/nv
成功解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See /var/log/nv
成功解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See  /var/log/nv
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
89 0
|
Java 数据库连接 mybatis
A query was run and no Result Maps were found for the Mapped Statement
A query was run and no Result Maps were found for the Mapped Statement
177 0
|
Web App开发 Ubuntu jenkins
Running Firefox as root in a regular user's session is not supported 问题解决
Running Firefox as root in a regular user's session is not supported 问题解决
758 0
Running Firefox as root in a regular user's session is not supported 问题解决
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
553 0
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
LD_LIBRARY_PATH shouldn&#39;t contain the current directory when building glibc. Please change the envir
执行# ./glibc-2.14/configure 出现以下错误: checking LD_LIBRARY_PATH variable... contains current directory configure: error: *** LD_LIBRARY_PATH ...
1802 0
|
PHP 开发工具
报错 invalid [default store dir]: /tmp/
php sdk解压以后文件如下   推荐处理方案    1.如果使用支付宝sdk,首先lotusphp_runtime 文件也要一起使用  支付宝现在的php sdk中有lotus框架可以和aop文件   夹放在同一级    2.
847 0