ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1119. Метро

C++ doesn't work, while Pascal does!
Послано jedimastex 2 июл 2004 22:54
this program isn't AC (WA test#10) because of the
precision of the answer, but Pascal solution using the
same ideas is AC. If you can help me to deal with the
precision I would be very grateful to you. Still i'd
recommend you to write the problem in Pascal to avoid all
potential precision problems :

#include <iostream>
#include <cmath>

using namespace std;

struct cross{int x,y;};

int main()
{
    int m,n,k,i;
    cross a[100];
    cin >> m >> n >> k;

    if (!k) {cout << 100*(m+n); return 0;}

    for (i=0;i<k;i++) cin >> a[i].x >> a[i].y;

    bool f=false;
    cross t;

    while (!f)
    {
        f=true;
        for (i=0;i<k-1;i++)
            if (a[i].x>a[i+1].x)
                {t=a[i]; a[i]=a[i+1]; a[i+1]=t; f=false;}
            else if (a[i].x==a[i+1].x && a[i].y>a[i+1].y)
                {t=a[i]; a[i]=a[i+1]; a[i+1]=t; f=false;}
    }

    int d[100];
    int mx,j;

    d[k-1]=1;
    for (i=k-2;i>=0;i--)
    {
        d[i]=1; mx=0;
        for (j=k-1;j>i;j--)
            if (a[i].x<a[j].x && a[i].y<a[j].y && d[j]>mx) mx=d[j];
        d[i]+=mx;
    }

    double ans;

    ans=(double)100*((double)m+(double)n-(double)2*d[0]+sqrt((double)2)*(double)d[0]);
    cout << floor(ans+(double).5);

    return 0;
}
Re: But I got AC in C#
Послано Tratata (barssimfi@mail.ru) 4 июл 2004 23:36
Re: But I got AC in C#
Послано asd 5 янв 2006 17:15
So what to do? I've got wa#10 and don't know how properly round the number!!! printf("%.0f",f); doesn't work!!!!
Re: But I got AC in C#
Послано jedimastex 6 янв 2006 17:54
I guess it would be easier to rewrite it in Delphi or Pascal. I didn't manage to find a mistake in my solution.
There're probably big random numbers in the bad test.
Pascal better
Послано Aydarkanov Tair OKTL 8 янв 2006 10:38
Because Pascal is better than your C++(or C)!
Pascal luchshe potomu shto tvoy C++ dibilniy
Re: Pascal better
Послано asd 8 янв 2006 12:53
In fact, I don't think so. I suppouse C++ is more powerfull, but probably test for this problem were made by Pascal program.
Re: Pascal better
Послано jedimastex 11 янв 2006 18:01
I cannot understand why you are so furious about C++.
If you know it well it's surely better than Pascal
(I mean it gives you much more freedom).
The only problem is to learn how to use its power properly.
Re: Pascal better
Послано Todor Tsonkov 12 июл 2006 02:47
Well, there are many arguments whether pascal or c++ is better  but I think it depends on the coder. Yet, I prefer C++ mainly because it's more powerful and it has STL which is a great advantage, although that lately I'm getting very fond of Java and once I couldn't solve a problem niether on C, c++ or pascal and I tried Java and suddenly made it :).
So sometimes tests really are made for some language
Re: But I got AC in C#
Послано Atik 29 янв 2008 21:59
function Round(X: Extended): Int64;

Description

In Delphi, the Round function rounds a real-type value to an integer-type value.

X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker’s Rounding".
Re: C++ doesn't work, while Pascal does!
Послано Mihran Hovsepyan {2 kurs of <RAU>} 29 янв 2009 23:28
this is very strange try to send this code before changing this fragment
d[k-1]=1;
for (i=k-2;i>=0;i--)
{
d[i]=1; mx=0;
for (j=k-1;j>i;j--)
if (a[i].x<a[j].x && a[i].y<a[j].y && d[j]>mx) mx=d[j];
d[i]+=mx;
}


to

d[0]=1;
for (i=1;i<k;i++)
{
d[i]=1; mx=0;
for (j=0;j<i;j++)
if (a[i].x>a[j].x && a[i].y>a[j].y && d[j]>mx) mx=d[j];
d[i]+=mx;
}

and this fragment

ans=(double)100*((double)m+(double)n-(double)2*d[0]+sqrt((double)2)*(double)d[0]);
cout << floor(ans+(double).5);


to

ans=100.0*(m+n-2*d[k-1])+d[k-1]*sqrt(20000.0);
    cout<<(int)(ans+0.5)<<endl;


and I hope you will get AC


Edited by author 29.01.2009 23:29
How to round in C++
Послано Georgi_georgiev 4 июн 2009 17:49
Well, I round with:
(int)(a + 0.5 + 1e-9);
When 'a' is the number I need to round.
Re: How to round in C++
Послано Team_Pixies 14 апр 2011 14:20
I think this method is better:

double ans;
/*
...
*/
cout.setf(ios::fixed,ios::floatfield);
cout.precision(0);
cout << ans;