CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

https://www.codechef.com/DEC17/problems/GIT01

#include<cstdio>
#include<algorithm>

using namespace std;

#define N 101

char s[N];

int main()
{
    int T;
    scanf("%d",&T);
    int n,m;
    int OddG,OddR,EvenG,EvenR;
    int ans;
    while(T--)
    {
        OddG=OddR=EvenG=EvenR=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
        {
            scanf("%s",s+1);
            for(int j=1;j<=m;++j)
            {
                if(s[j]==‘G‘)
                {
                    if((i+j)&1) OddG++;
                    else EvenG++;
                }
                else
                {
                    if((i+j)&1) OddR++;
                    else EvenR++;
                }
            }
        }
        ans=5*OddR+3*EvenG;
        ans=min(ans,3*OddG+5*EvenR);
        printf("%d\n",ans);
    }
} 

All submissions for this problem are available.

Read problems statements in Mandarin chineseRussian andVietnamese as well.

Chef’s girlfriend‘s birthday is near, so he wants to surprise her by making a special cake for her. Chef knows that his girlfriend likes cherries on the cake, so he puts cherries on the top of the cake, but he was not satisfied. Therefore, he decided to replace some of the cherries to make a beautiful pattern. However, Chef has a lot of other work to do so he decided to ask for your help.

The cherries are of two colors red and green. Now Chef wants the cherries to be placed in such a way that each cherry of one color must be adjacent to only cherries of the other color, two cherries are adjacent if they share a side. Now Chef has asked for your help in making that pattern on the cake.

You can replace any cherry of given color with the other color. But there is a cost for each replacement: if you replace a red cherry with a green one, the cost is 5 units and if you replace a green cherry with a red one, the cost is 3 units.

Help your friend Chef by making the cake special with minimum cost.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and M, where N × M are the dimensions of the cake.
  • Each of the next N lines contains a string of length M.

Output

For each test case, output the minimum cost required to make the cake special.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N, M ≤ 100
  • each string consists only of letters ‘R‘ and ‘G‘ denoting red and green cherries respectively

Example

Input:

2
4 5
RGRGR
GRGRG
RGRGR
GRGRG
2 3
RRG
GGR

Output:

0
8
时间: 2024-11-05 02:30:41

CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake的相关文章

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 100001 int a[N],b[N]; int sum[N],wh[N][2]; int num1[N],num2[N]; void read(int &x) { x=0; char c=getc

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 1000001 long long sum[N*2],dp[N]; int num[11]; void read(int &x) { x=0; char c=getchar(); while(!isdi

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out

https://www.codechef.com/DEC17/problems/CPLAY #include<cstdio> #include<algorithm> using namespace std; char s[21]; int main() { int sumA,sumB; while(scanf("%s",s+1)!=EOF) { sumA=sumB=0; int i; for(i=1;i<=10;++i) { if(i&1) { i

CF&amp;&amp;CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 然后可以对区间剩余数重排 继续删除下标为等差数列且数值相等的一些数 继续对区间进行重排 直至区间内没有数 问每次操作的最少删除次数 因为每次删除后可以重排 那么完全可以在第一次删除后就把区间相等的数排在一起 这样相等的数的下标就分别构成了公差为1的等差数列 所以问题转化为 设区间[l,r]内数有x

CF&amp;&amp;CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的数都下取整,累积更改的sum 那么选1个小数上取整,就会使sum-1 整数上下取整不会产生影响 所以有1个整数就可以使上取整的小数少1个 所以ans=min(abs(i-sum)) i∈[n-整数个数,n] #include<cmath> #include<cstdio> #inclu

Codechef December Challenge 2014 Chef and Apple Trees 水题

Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare a special dish for you, and needs to gather several apples to do so. Chef has N apple trees in his home garden. Each tree has a certain (non-zero) num

[codechef July Challenge 2017] Chef and Sign Sequences

CHEFSIGN: 大厨与符号序列题目描述大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含'<'.'='和'>'三种比较符号的字符串.记字符串长度为 N,大厨想要在字符串的开头.结尾,和每两个字符之间插入一个正整数,共N + 1 个数.大厨希望插入数字之后,这些比较符号所表达的含义是正确的.举个例子,如果在'<'前后分别插入 a 和 b,那么应当有 a < b.对于'='和'>'也是类似的.大厨可以在 [1, P] 中任意选择数字插入,同一个数也可以被插入到多个位置.请你帮

[codechef July Challenge 2017] IPC Trainers

IPCTRAIN: 训练营教练题目描述本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 Di 天到达,直到训练营结束才离开.第 i 名教练希望上 Ti 节课.要是少上了课,那么教练会感到扎心,每少上一节,扎心值就会加 Si.作为主办方,你希望最小化所有教练的扎心值之和.输入格式输入的第一行包含一个整数 T,代表测试数据的组数.接下来是 T 组数据.每组数据的第一行包含两个整数 N 和

[codechef July Challenge 2017] Calculator

CALC: 计算器题目描述大厨有一个计算器,计算器上有两个屏幕和两个按钮.初始时每个屏幕上显示的都是 0.每按一次第一个按钮,就会让第一个屏幕上显示的数字加 1,同时消耗 1 单位的能量.每按一次第二个按钮,会让第二个屏幕上显示的数字加上第一个屏幕上显示的数字,同时消耗 B 单位的能量.初始时,计算器有 N 单位的能量.大厨想知道在能量限制下,第二个屏幕上最大可以出现的数字是多少?输入格式输入的第一行包含一个整数 T,代表测试数据的组数.接下来是 T 组数据.每组数据仅有一行,包含两个整数 N