Numbers on the Chessboard- Educational Codeforces Round 49 (Rated for Div. 2)B题

思路:
找规律即可。

AC code:
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n = 0,q = 0;
    while(cin >> n >> q){
        int me = 0,mo = 0;
        int start = 0;
        if(n % 2 == 0){
             me = n/2,mo = n/2;
             start = n*n / 2 + 1;
        }else{
            me = (n+1)/2,mo = (n-1)/2;
            start = (n*n + 1)/2+1;
        }
        int x = 0,y = 0;
        for(int i = 0;i < q;++i){
            cin >> x >> y;
            if(n % 2 == 0){
                if((x + y) % 2 == 0){
                    cout << 1 + (x-1)*me + (y-1)/2 <<endl;
                }else{
                    cout << start + (x - 1)*mo + (y - 1)/2 << endl;
                }
            }else{
                if((x + y) % 2 == 0){
                    int sum = (x - 1)/2 * n + x%2 == 0? me : 0;
                    //cout << "en " << sum << endl;
                    cout << 1 + sum + (y-1)/2 <<endl;
                }else{
                    int sum = (x - 1)/2 * n + x%2 == 0? mo : 0;
                    //cout << "en " << sum << endl;
                    cout << start + sum + (y-1)/2 <<endl;
                }   
            }
        }

    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值