求助,用了DP 为啥TLE?

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

emoheizi @ 2020-06-10 18:45:26

代码如下:


#define MAX_VAL 1000+5
#include <bits/stdc++.h>
using namespace std;
int n;
int curs[MAX_VAL];

inline void redn(int& res){
    char ch=getchar();int cof = 1; res=0;
    while(ch>'9'||ch<'0') if(ch=='-') cof = -1,ch=getchar();
    while(ch>='0'&&ch<='9') res = res*10 + (ch-'0'),ch=getchar();;
    res *= cof;
}

int main(int arg,char **argv){
    redn(n);
    int temp;
    for(int i=n;i>0;i--)
        for(int j=i;j<=n;j++) redn(temp),curs[j] = max(curs[j],curs[j+1]) + temp;
    int ans = 0;
    for(int i=1;i<=n;i++)
        ans = max(ans,curs[i]);
    cout<<ans;
    return 0;
}

by emoheizi @ 2020-06-10 18:47:10

全部用例都TLE.... 时间1.2s,题目限制1s


by _SAR_ @ 2020-06-10 18:48:12

很怪……


by Ryo_Yamada @ 2020-06-10 18:51:37

@emoheizi 快读写错了……


by Ryo_Yamada @ 2020-06-10 18:52:27

while(ch>'9'||ch<'0') if(ch=='-') cof = -1,ch=getchar();

这样的话只有是负数的时候才不会错,因为ch=getchar()写在if里面了


by _SAR_ @ 2020-06-10 18:52:38

(原谅我删掉了你的快读再看的代码= =)


by xhQYm @ 2020-06-10 18:52:55

改成这样AC了


#define MAX_VAL 1000+5
#include <bits/stdc++.h>
using namespace std;
int n;
int curs[MAX_VAL];

int main(){
    cin>>n;
    int temp;
    for(int i=n;i>0;i--)
        for(int j=i;j<=n;j++) cin>>temp,curs[j] = max(curs[j],curs[j+1]) + temp;
    int ans = 0;
    for(int i=1;i<=n;i++)
        ans = max(ans,curs[i]);
    cout<<ans;
    return 0;
}

好像是快读的锅


by xhQYm @ 2020-06-10 18:53:15

@breeze末影 艹手慢了艹


by Ryo_Yamada @ 2020-06-10 18:53:48

@qym2008 哈哈哈哈哈(大雾逃


by emoheizi @ 2020-06-10 18:55:48

@breeze末影 十分感谢


by emoheizi @ 2020-06-10 18:56:03

@qym2008 十分感谢!


|