• Ehab and subtraction(思维题)


    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You're given an array aa. You should repeat the following operation kk times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.

    Input

    The first line contains integers nn and kk (1≤n,k≤105)(1≤n,k≤105), the length of the array and the number of operations you should perform.

    The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), the elements of the array.

    Output

    Print the minimum non-zero element before each operation in a new line.

    Examples

    input

    Copy

    3 5
    1 2 3
    

    output

    Copy

    1
    1
    1
    0
    0
    

    input

    Copy

    4 2
    10 3 5 3
    

    output

    Copy

    3
    2
    

    Note

    In the first sample:

    In the first step: the array is [1,2,3][1,2,3], so the minimum non-zero element is 1.

    In the second step: the array is [0,1,2][0,1,2], so the minimum non-zero element is 1.

    In the third step: the array is [0,0,1][0,0,1], so the minimum non-zero element is 1.

    In the fourth and fifth step: the array is [0,0,0][0,0,0], so we printed 0.

    In the second sample:

    In the first step: the array is [10,3,5,3][10,3,5,3], so the minimum non-zero element is 3.

    In the second step: the array is [7,0,2,0][7,0,2,0], so the minimum non-zero element is 2.

    题解:如果直接暴力去找的话必然是会超时的,我们可以利用c++排序的功能排序好,然后进行比较每次输出最小的就大大提高了效率,从而不会是超时

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    int main()
    {
    	
    	int n,m;
    	cin>>n>>m;
    	int a[100005];
    	for(int t=0;t<n;t++)
    	{
    		scanf("%d",&a[t]);
    	}
        sort(a,a+n);
        int k=0;
        int temp=0;
        for(int t=0;t<m;t++)
        {
        	while(k!=n-1&&a[k]==temp)k++;
        	printf("%d
    ",a[k]-temp);
        	temp=a[k];
    	}
    	return 0;
    }
  • 相关阅读:
    RN8209校正软件开发心得(1)
    Chrome 31版本导出Excel问题
    ComBox选择
    网页设计的一般步骤
    .NET一套开发工具
    关于用sql语句实现一串数字位数不足在左侧补0的技巧
    python jieba模块详解
    python内置函数详细描述与实例演示
    Markdown的基本语法记录
    python configparser模块详解
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781952.html
Copyright © 2020-2023  润新知