HAOI2014 走出金字塔

题目链接:戳我

找规律。

不过为了方便,每次我们计算入口和某一个出口之间需要花费的体力值的时候,不妨把x较小的假设成塔顶,这样的话另一个就不需要分类讨论了。

详细请看代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define MAXN 1000010
using namespace std;
int n,m,r,s,x,y,xx,yy;
int ans=2147483647,cur_ans;
struct Node{int x,y;}node[MAXN];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d%d%d%d",&n,&m,&r,&s);
    for(int i=0;i<=m;i++)
        scanf("%d%d",&node[i].x,&node[i].y);
    for(int i=1;i<=n;i++)
    {
        x=node[i].x,y=node[i].y;
        xx=node[0].x,yy=node[0].y;
        cur_ans=0;
        if(x<xx) swap(xx,x),swap(yy,y);
        if(y%2==0) x--,y--,cur_ans++;
        if(yy%2==0) xx--,yy--,cur_ans--;
        x-=xx; y-=yy-1;
        if(y<1) cur_ans+=1-y;
        if(y>(2*(x+1)-1)) cur_ans+=y-2*(x+1)+1;
        cur_ans+=2*x;
        ans=min(ans,cur_ans);
    }
    ans*=r;
    if(ans+1<=s) printf("%d
",s-ans-1);
    else printf("-1
");
    return 0;	
}

原文地址:https://www.cnblogs.com/fengxunling/p/10462517.html