SPOJ" ABS(A-B)I"错误答案的问题 [英] SPOJ "abs(a-b) I" Wrong Answer issue

查看:184
本文介绍了SPOJ" ABS(A-B)I"错误答案的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的做法的问题陈述 http://www.spoj.com/problems/ABSP1 / - 请检查是否有任何一个角落的情况下在其上我的code在下降,因为根据我的测试情况下,给予正确的答案

问题描述:

  

您给出N个数的非递减顺序的数组。你有   回答的所有不同的绝对差的总和   对给定的数组中开始。

的scanf(%d个,和放大器; TotalElements);

 为(i = 0; I< TotalElements;我++)
       scanf函数(%d个,和放大器;数组[I]);

    FirstSum = TotalSum = 0;
    对于(i = 0; I< TotalElements;我++)
       FirstSum + = ABS(数组[我] -array [0]);

    TotalSum = FirstSum;
    SumTillNow =数组[0];
    对于(i = 1; I< TotalElements;我++){

        差=数组[I] -array [0];
        NextSum = FirstSum差分*(TotalElements-ⅰ)-SumTillNow +(ⅰ)*数组[0];
        TotalSum + = NextSum;
        SumTillNow + =数组[我]

    }

    的printf(%LLD \ N,TotalSum);
 

解决方案

问题制定者似乎并不具备足够的经验。这里的原因:

  1. 他说,不同的对,但它似乎并不喜欢它。也许他的意思说的是无序对。

  2. 测试数据不符合的约束。断言对输入数据进行验证。使用64位有符号整数输入。

请改变你的计划保持这两个点记(尤其是第2点),它应该得到认可。

This is my approach for the problem statement http://www.spoj.com/problems/ABSP1/ - please check if there is any corner case on which my code is falling because according to my test cases it is giving correct answer.

Problem Statement:

You are given an array of N numbers in non-decreasing order. You have to answer the summation of the absolute difference of all distinct pairs in the given array.

scanf("%d",&TotalElements);

    for(i=0;i<TotalElements;i++)
       scanf("%d",&Array[i]);

    FirstSum=TotalSum=0;        
    for(i=0;i<TotalElements;i++)
       FirstSum+=abs(Array[i]-Array[0]);

    TotalSum=FirstSum;
    SumTillNow=Array[0];
    for(i=1;i<TotalElements;i++){

        Difference=Array[i]-Array[0];
        NextSum=FirstSum-Difference*(TotalElements-i)-SumTillNow+(i)*Array[0];          
        TotalSum+=NextSum;
        SumTillNow+=Array[i];

    }

    printf("%lld\n",TotalSum);  

解决方案

The problem setter doesn't seem to have enough experience. Here's why:

  1. He says distinct pairs but it doesn't seem like it. Maybe what he meant to say was UNORDERED pairs.

  2. The test data doesn't conform to the constraints. Asserts on input data verify this. Use 64 bit signed integers for input.

Make changes to your program keeping these two point in mind (especially point 2) and it should get accepted.

这篇关于SPOJ&QUOT; ABS(A-B)I&QUOT;错误答案的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆