注册 登录
编程论坛 C++教室

onlinejudge 上面的题目有问题。。1045.分段线性插值

聪儿 发布于 2012-10-19 19:36, 369 次点击
#include<iostream>
#define MAXSIZE 200000
#define START -1000000000
#define END 1000000000
using namespace std;

struct
{
    int x;
    int y;
}a[MAXSIZE];
double output[MAXSIZE];
int acount=0;
int ocount=0;

int sort(int x,int y)
{
    int j=acount-1;
    if(acount==0)
    {
        a[acount].x=x;
        a[acount].y=y;
    }

    else for(int i=0;i<acount;i++)
    {
        if(x>a[acount-1].x)
        {
            a[acount].x=x;
            a[acount].y=y;
        }
        else     while (x<a[j].x&&j>=0)
        {
            a[j+1].x=a[j].x;
            a[j+1].y=a[j].y;
            j--;
        }
        if(x==a[j].x) return 0;
        a[j+1].x=x;
        a[j+1].y=y;
    }
    return 1;
}

void camulate(int x)
{
    int x1,x2,y1,y2;
    x1=START;
    x2=END;
    y1=y2=0;
    for(int i=0;i<acount;i++)
    {
        if(x<a[0].x)
        {
            x1=START;
            y1=0;
            x2=a[0].x;
            y2=a[0].y;
            break;
        }
        else if(x<a[i].x)
        {
             x1=a[i-1].x;
             y1=a[i-1].y;
             x2=a[i].x;
             y2=a[i].y;
            break;
        }
        else
        {
            x1=a[i].x;
            y1=a[i].y;
            x2=END;
            y2=0;
        }
    }
    double h=x1-x2;
    output[ocount]=y1*(x-x2)/h-y2*(x-x1)/h;
}

int main()
{
    int n,x,y;
    char ch;
    cin>>n;
    if(n<=0||n>MAXSIZE) return -1;
    ch=getchar();
    ch=getchar();
    while((acount+ocount)<n)
    {
        if(ch=='A')   
        {   
            scanf("%d%d",&x,&y);
            if(x<=START||x>=END||y<=START||y>=END) return -1;
            if (! sort(x,y)) return -1;
            acount++;
        }
   
        else if(ch=='Q')   
        {   
            scanf("%d",&x);
            if(x<=START||x>=END) return -1;
            camulate(x);   
            ocount++;
        }

        ch=getchar();
    }

    for(int i=0;i<ocount;i++)
        printf("%0.6lf\n",output[i]);
    return 0;
}


这个是我的完整代码,这个是题目http://acm.xmu.
我不明白为什么结果是re。test2出错。
我看了一下accepted的 程序,基本上 内存都用到3000K左右,而我的基本上就是不到100K ,现在也想不通为什么也用到那么大的内存,求懂得的大牛稍微指点一下我的疏漏之处。非常感谢!!
8 回复
#2
聪儿2012-10-19 19:37
另外,我感觉我的代码非常庞大,如果有什么可以优化的建议拜托提出来。。
非常感谢哦 ^_^
#3
rjsp2012-10-19 23:50
随手这么一写,你检查一下是否有错误

程序代码:
#include <map>
#include <cstdio>

struct foo
{
    foo()
    {
        xys.insert( std::make_pair(-1000000000,0) );
        xys.insert( std::make_pair(+1000000000,0) );
    }
    void add( int x, int y )
    {
        xys.insert( std::make_pair(x,y) );
    }
    double query( int x ) const
    {
        std::map<int,int>::const_iterator itor = xys.lower_bound( x );
        int x2=itor->first, y2=itor->second;
        --itor;
        int x1=itor->first, y1=itor->second;
        return y1 + (x-x1+0.0)*(y2-y1)/(x2-x1);
    }
private:
    std::map<int,int> xys;
};

int main()
{
    foo a;

    int n = 0;
    scanf( "%d", &n );
    for( int i=0; i<n; ++i )
    {
        char c;
        scanf( "%*c%c", &c );
        switch( c )
        {
        case 'A':
            {
                int x, y;
                scanf( "%d%d", &x, &y );
                a.add( x, y );
            }
            break;
        case 'Q':
            {
                int x;
                scanf( "%d", &x );
                double y = a.query(x);
                printf( "%f\n", y );
            }
            break;
        }
    }

    return 0;
}

#4
聪儿2012-10-20 14:53
可以帮忙看下我的代码的问题吗?
这个是主要的。。。
多谢啦……
#5
聪儿2012-10-22 10:16
回复 3楼 rjsp
不好意思,你的连最基本的sample测试都过不去。
#6
rjsp2012-10-23 08:01
回复 5楼 聪儿
测试不过的话,告诉我
你输入什么
期待输出什么
实际输出什么
#7
聪儿2012-11-02 09:20
回复 6楼 rjsp
http://acm.xmu.

这里是题目。

我不知道是我对题目的理解有问题还是你的理解不对。我感觉输出的格式应该是 输入完成之后,一次性输出。而你的输出格式是 每当有Q出现,输出一次。

我的输出是:
6
 Q 5
 A -10 10
 A 10 20
 Q 5
 A 0 0
 Q 5
0.000000
17.500000
10.000000
Press any key to continue

你的输出是:
6
Q 5
0.000000
A -10 10
A 10 20
Q 5
17.500000
A 0 0
Q 5
10.000000
Press any key to continue

而且我的程序运行时可以直接把那个sample里面的输入贴进去,你的就不行。。
#8
聪儿2012-11-02 09:21
这个程序困扰我非常久了。。。麻烦稍微详细的解释下!
非常感谢!
#9
聪儿2012-11-03 16:24
版主还在的话,静候您的指教。。
1