zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Kafka:主要参数详解(转)
    CXF:根据werservice代码生成WSDL(转)
    CentOS:ECDSA host key "ip地址" for has changed and you have requested strict checking(转)
    Oracle:一个用户操作多个表空间中表的问题(转)
    CentOS:设置系统级代理(转)
    Apache2.4:AH01630 client denied by server configuration
    Amabri:如何删除或停止指定的服务
    08 操作符相关
    07 Test结构
    06 退出 退出状态
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781952.html
Copyright © 2011-2022 走看看