时间: 2020-11-25|60次围观|0 条评论

题目传送门:https://codeforces.com/contest/1036/problem/B

被这道题坑了,说白了还是菜。

贪心策略是先斜对角从(0,0)走到(n,n),然后往右拐(分奇偶考虑)【若n>m,swap(n,m)】

理论上是画画图,知道切入点是奇偶性后,就能想清楚了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#define INF 2e9
using namespace std;



int main(){
    ios::sync_with_stdio(false);
    int q; cin>>q;
    while(q--){
        long long n,m,k; cin>>n>>m>>k;
        //from (0,0) to (n,m)
        if(n>m) swap(m,n);
        // get n<=m
        
        //from (0,0) to (n,n)
        long long cnt=n;
        if( (m-n)%2==1 ){
   //from (n,n) to (n,m-1)
            cnt+=m-n-1;
            k-=cnt;
            if(k<=0){ cout<<-1<<endl; }
            else{
                k-=1;
                cnt+=k;
                cout<<cnt<<endl;
            }
        }
        else{
   //from (n,n) to (n,m)
            cnt+=m-n;
            k-=cnt;
            if( k<0 ) cout<<-1<<endl;
            else if( k==0 ) cout<<cnt<<endl;
            else{
                long long zero=0;
                if(k%2==0) cout<<cnt+k<<endl;
                else{
                    if(k==1) cout<<cnt-1<<endl;
                    else cout<<cnt+k-2<<endl;
                }
            }
        }
    }
    
    return 0;    
}

 

转载于:https://www.cnblogs.com/ZhenghangHu/p/9678767.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/96631079

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《Codeforces 1036B Diagonal Walking v.2 【贪心】
   

还没有人抢沙发呢~