zoukankan      html  css  js  c++  java
  • SORT AGAIN

    SORT AGAIN

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6278    Accepted Submission(s): 1984


    Problem Description
    给你N个整数,x1,x2...xn,任取两个整数组合得到|xi-xj|,(0<i,j<=N,i!=j)。
    现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它)。
     
    Input
    输入数据首先包含一个正整数C,表示包含C组测试用例.
    每组测试数据的第一行包含两个整数N,K。(1<N<=1000,0<K<=2000)
    接下去一行包含N个整数,代表x1,x2..xn。(0<=xi<=2000)
     
    Output
    对于每组测试数据,请输出第K大的组合数,每个输出实例占一行。
     
    Sample Input
    3
    3 2
    4 0 7
    4 2
    1 2 3 4
    2 1
    2 9
     
    Sample Output
    4
    2
    7
     
    一开始用set竟然超时,顿时无语,然后想了这个方法。
    其实总的来说这题还是一般的。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <set>
     6 #define mem(a) memset(a,0,sizeof(a))
     7 using namespace std;
     8 int a[1005],ans[2005];
     9 int main(){
    10     int n;
    11     scanf("%d",&n);
    12     while(n--){
    13         mem(a);
    14         mem(ans);
    15         int x,k;
    16         scanf("%d%d",&x,&k);
    17         for(int i=0;i<x;i++){
    18             scanf("%d",&a[i]);
    19         }
    20         sort(a,a+x);
    21         for(int i=0;i<x;i++){
    22             for(int j=i+1;j<x;j++){
    23                 int p=abs(a[j]-a[i]);
    24                     ans[p]++;
    25             }
    26         }
    27         int b=0;
    28        for(int i=0;i<=2000;i++){
    29            if(ans[i]){
    30                b++;
    31                if(b==k){
    32                    cout<<i<<endl;
    33                    break;
    34                }
    35            }
    36        }
    37 
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    three.js
    three.js
    three.js
    反射API提供的常用类和函数
    PHP控制反转(IOC)和依赖注入(DI)
    优化思路以及优化过程
    nginx的缓存设置提高性能
    网页内容的压缩编码与传输速度优化
    nginx日志按日期自动切割脚本
    mysql数据备份
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7341845.html
Copyright © 2011-2022 走看看