zoukankan      html  css  js  c++  java
  • 删除

    【题目描述】

    现在,我的 上有 n 个数字,分别是 a1; a2; a3; :::; an。 我现在需要删除其中的 k 个数字。当然我不希望随随便便删除,我希望删除 k

    数字之后,剩下的 n   k 个数中有最多的不同的数

    【输入格式】

    第 两个正整数 nk,含义如题 描述。 接下来  ,有 n 个 负整数,分别是 a1an

    【输出格式】

    共  , 个整数 ans,表   删除了 k 个数字后最多的不同的数的个数。

    【样例输入】

    4 1

    1 3 1 2

    【样例输出】

    3

    【样例解释】

    如果删去第 个 1: 在[3,1,2]中有 3 个不同的数 如果删去 3:

    在[1,1,2]中有 2 个不同的数 如果删去第 个 1: 在[1,3,2]中有 3 个不同的数

    【解题思路】

    排序去重什么的,然后判断一下有木有比k大,大概就是这个样子

    
    
     1 program del;
     2 var a:array[0..100000] of longint;
     3      i,j,n,k,ans:Longint;
     4  procedure sort(l,r: longint);
     5       var
     6          i,j,x,y: longint;
     7       begin
     8          i:=l;
     9          j:=r;
    10          x:=a[(l+r) div 2];
    11          repeat
    12            while a[i]<x do
    13             inc(i);
    14            while x<a[j] do
    15             dec(j);
    16            if not(i>j) then
    17              begin
    18                 y:=a[i];
    19                 a[i]:=a[j];
    20                 a[j]:=y;
    21                 inc(i);
    22                 j:=j-1;
    23              end;
    24          until i>j;
    25          if l<j then
    26            sort(l,j);
    27          if i<r then
    28            sort(i,r);
    29       end;
    30 begin
    31     assign(input,'del.in'); reset(input);
    32     assign(output,'del.ans'); rewrite(output);
    33     read(n,k);
    34     ans:=n;
    35     for i:=1 to n do read(a[i]);
    36     sort(1,n);
    37     for i:=1 to n do
    38     begin
    39         if a[i]=a[i-1] then
    40         begin
    41             if k>0 then dec(k);
    42             dec(ans);
    43         end;
    44     end;
    45     writeln(ans-k);
    46     close(input);
    47     close(output);
    48 end.
    
    
    
     
  • 相关阅读:
    Windows UI自动化测试的XPATH实现
    Laravel修炼:服务容器绑定与解析
    swoole之memoryGlobal内存池分析
    Go语言的前景分析
    thinkphp5 编辑时 唯一验证 解决办法
    GIT配置多用户
    PHP 数组
    PHP 变量作用域
    PHP 使用 Swoole
    欢迎使用CSDN-markdown编辑器
  • 原文地址:https://www.cnblogs.com/wuminyan/p/4744007.html
Copyright © 2011-2022 走看看