C# CodinGame记录(5)--Horse-racing Duals

一群马,返回马数值差值最小的数

一开始想到是穷举暴力,但是超时了。

后来想着先排序,去整了快速排序如下

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution
{
    static void Main(string[] args)
    {
        
        int N = int.Parse(Console.ReadLine());
        int[] pi = new int[N+1];
        for (int i = 1; i <= N; i++)
        {
            pi[i] = int.Parse(Console.ReadLine());
        }
        Qsort (pi,1,N);
        int sm=pi[2]-pi[1];
        for(int i=2;i <= N;i++)
        {
            if(pi[i]-pi[i-1]< sm){sm=pi[i]-pi[i-1];}
        }
        // Write an answer using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        Console.WriteLine(sm);
    }
    static void Qsort (int[] a,int low,int high){
            if(low<high)
            {
                int i=low;
                int j=high;
                a[0] = a[low];
                while(low < high){
                    while(a[high]>a[0] && low < high){
                        if (a[0]<= a[high]){
                            --high;
                        }else{a[low]=a[high];break;}                   
                    }
                    while(a[low]<a[0] && low < high){
                        if (a[0] > a[low]){
                            ++low;
                        }else{a[high]=a[low];break;}
                    }
                }
                a[low] =a[0];
                Qsort (a,i,low-1);
                Qsort (a,high+1,j);
            }
    }
}

 虽然能过其中几个测试,但是数量>10就还是超时(这里O(nlogn)),先放着吧,之后尝试别的排序

 反正浪费的空间也不是我的空间是吧(笑)


大半年过去了

来填坑

我以前真捞

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
class Solution
{
    static void Main(string[] args)
    {
        int diff;
        int N = int.Parse(Console.ReadLine());
        int[] pi = new int[N];
        for (int i = 0; i < N; i++)
        {
            pi[i] = int.Parse(Console.ReadLine());
            Console.Error.WriteLine(pi[i]);
        }
        Array.Sort(pi);

        // Write an answer using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");
        diff = pi[1]-pi[0];
        for(int i =2 ;i<N;i++){
            if(diff>pi[i]-pi[i-1])diff =pi[i]-pi[i-1];
        }
        Console.WriteLine(diff);
    }


}

直接解决了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值