D-阿强与网格

题目链接 : 

阿强与网络

思路 : 

数学模拟;

D-阿强与网格_第1张图片

 详情请看代码 :

代码 :

#include
#include
using namespace std;
typedef long long LL;
int main(){
    int t ; 
    scanf("%d",&t);
    LL ans,m,n,x,y;
    while(t--){
        scanf("%lld%lld%lld%lld",&m,&n,&x,&y);
        if(m==1 || n==1)  {
            printf("%lld\n",(max(m,n)-1)*x);
            continue ;
        }
        if(2*x<=y) ans = (LL)(m+n-2)*x;
        else{
            long long  ma = max(m,n) , mi = min(m,n); 
            if(x>y) ans=(mi-1)*y+(ma-mi)/2*2*y+((ma-mi)%2)*x;
            else ans = (mi-1)*y + (ma-mi) *x; 
        }
        printf("%lld\n",ans);
    }
}

你可能感兴趣的:(算法学习,NowCoder,算法)