Codeforce961C(暴力)

本文介绍了一个关于修复2*nX2*n大小的混乱棋盘的问题。一个完整的棋盘被分为四个n*n大小的部分,颜色分布混乱。文章探讨了如何重新排列这些部分并更改最少数量的方格颜色以恢复棋盘原有的黑白相间模式。

Chessboard

Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what’s even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
Input
The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.

题意:有一个2*n X 2*n大小的棋盘,棋盘上的颜色有两种它们分别用1和0表示,它们的位置相互交错。现在有一个棋盘分裂为4个n*n大小的块,并且上面的颜色已经乱了,要求你把这些块拼在一起,要求求出使棋盘颜色符合要求所需改颜色的次数最少是多少。
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 1000000000
using namespace std;
typedef long long ll;
const int MAXN=1e9+10;
const int MAX=1e5+10;
const double eps=1e-6;

int n;
char s[5][110][110];

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif

    cin>>n;
    for(int k=1;k<=4;k++)
        for(int i=0;i<n;i++)
                scanf("%s",&s[k][i]);
    int ans=100000;
    for(int k=1;k<=4;k++){
        for(int h=k+1;h<=4;h++){
            int y,x;
            x=y=0;
            for(int i=1;i<=4;i++)
                if(i!=h&&i!=k){
                    if(!x)  x=i;
                    else    y=i;
                }
            int temp=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if((i%2==1&&j%2==1)||(i%2==0&&j%2==0)){
                        if(s[k][i][j]!='1')   temp++;
                        if(s[h][i][j]!='1')   temp++;
                        if(s[x][i][j]!='0')   temp++;
                        if(s[y][i][j]!='0')   temp++;

                    }
                    else{
                        if(s[k][i][j]!='0')   temp++;
                        if(s[h][i][j]!='0')   temp++;
                        if(s[x][i][j]!='1')   temp++;
                        if(s[y][i][j]!='1')   temp++;
                    }
                }
            }
            ans=min(ans,temp);
        }
    }
    cout<<ans<<endl;

    return 0;
}
### 解题思路 在Codeforces 617C浇花问题里,目标是确定在给定天数、花的初始高度、每次可浇水的连续花的数量等条件下,能让最小高度的花达到的最大高度。这是一个优化问题,核心在于找到一种浇水策略,使得最终最小高度的花尽可能高。 一般可采用二分查找的方法来解决。先确定最小高度花的可能取值范围,也就是从当前最小高度到一个理论上可能达到的最大高度。然后在这个范围内进行二分查找,对于每一个中间值,检查是否能够通过合理的浇水操作让所有花的高度都不低于这个中间值。 检查的过程是模拟浇水操作,从左到右遍历每一朵花,如果某朵花的高度低于当前检查的中间值,就从这朵花开始往后选择连续的 `w` 朵花进行浇水,直到这朵花达到中间值。同时要记录浇水的次数,若浇水次数超过了给定的天数 `m`,则说明这个中间值无法达到,需要缩小右边界;反之,则可以尝试更大的值,扩大左边界。 ### 代码实现 ```python # 检查是否能让所有花的高度都不低于 h def can_reach_height(heights, m, w, h): n = len(heights) watered = [0] * n total_watering = 0 current_watering = 0 for i in range(n): # 计算当前位置的实际浇水情况 if i - w >= 0: current_watering -= watered[i - w] need = max(0, h - (heights[i] + current_watering)) watered[i] = need current_watering += need total_watering += need if total_watering > m: return False return True # 二分查找最大的最小高度 def find_max_min_height(heights, m, w): left = min(heights) right = min(heights) + m while left < right: mid = (left + right + 1) // 2 if can_reach_height(heights, m, w, mid): left = mid else: right = mid - 1 return left # 读取输入 n, m, w = map(int, input().split()) heights = list(map(int, input().split())) # 计算并输出结果 result = find_max_min_height(heights, m, w) print(result) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值