hdu 2073: Unlimited Road (thinking)

hdu 2073: Unlimited Road (thinking)

Topic Link

Start thinking about using a prefix, then you can call back directly, but because of limited strength, could not do it, thinking back on the violence altogether simulation results pay out of TLE. . . . .

So I began to see the bigwigs of the blog

https://blog.csdn.net/yangyafeiac/article/details/7828435

Actually is a thinking problem, I feel it is quite difficult to think of, in fact, I think the prefix actually possible, but that did not get out

A little lost ...

AC Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
double fx(int x,int y){
    double ans=0.0;
    double m=sqrt(2.0);
    int n=x+y;
    for(int i=1;i<n;i++){
        ans+=i*m*1.0;
    }
    ans+=x*m;           //这里重点注意
    for(int i=0;i<n;i++)
        ans+=sqrt(i*i*1.0+(i+1)*(i+1)*1.0);
    return ans;
}
int main(){
    int t;
    int x1,y1,x2,y2;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("%.3f\n",fabs(fx(x1,y1)-fx(x2,y2)));
    }
    return 0;
}

TLE Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define ll long long
using namespace std;
int main(){
    int k;
    int x1,y1,x2,y2;
    scanf("%d",&k);
    while(k--){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if((x1+y1)>=(x2+y2)){
            int t=x1;
            x1=x2;
            x2=t;
            t=y1;
            y1=y2;
            y2=t;
        }
        int i=x1,j=y1;
        double sum=0.0;
        while(!(i==x2&&j==y2)){
            if(i==0&&j==0){
                sum+=1.0;
                j++;
            }
            else if(j==0){
                sum+=sqrt(i*i*1.0+(i+1)*(i+1)*1.0);
                j=i+1;
                i=0;
            }
            else{
                sum+=sqrt(2.0);
                i++;
                j--;
            }
        }
        printf("%.3f\n",sum);
    }
    return 0;
}
Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/102644934