CodeChef 2016年04月问题

COLOR

(1)问题描述:Chef has N rooms, each of them painted in one of 3 colors (R, G or B). He wants to repaint the minimum number of rooms such that, in the end, all the rooms have the same color.(R+B->G,B+G->R,G+R->B)

(2)要点:任意一个颜色可以paint一次之后变成任意另外一个颜色

(3)代码:

#include <stdio.h>

int main()
{

    static const size_t buff_size = 100000;
    char buff[buff_size+1] = { 0 };
    unsigned int nCases = 0;scanf("%d",&nCases);
    for(unsigned int iCases = 1;iCases <= nCases;++iCases)
    {
        unsigned int len = 0;scanf("%d%s",&len,buff);
        size_t rcount = 0,gcount = 0,bcount = 0;
        for(unsigned int i = 0;i < len;++i)
        {
            rcount += ('R' == buff[i]);
            gcount += ('G' == buff[i]);
            bcount += ('B' == buff[i]);
        }
        unsigned int ans = len,maxv = rcount;
        if(gcount > maxv) maxv = gcount;
        if(bcount > maxv) maxv = bcount;
        ans -= maxv;
        printf("%u\n",ans);
    }
    return 0;
}


CHBLLNS

(1)问题描述:Chef must draw the minimum number of balloons from a box containing R red, G green and B blue balloons, in such a way that he is certain that there will be at least K balloons of the same color among the drawn balloons.

(2)要点:抽屉原理

(3)代码:

#include <stdio.h>
#include <assert.h>

int main()
{
    unsigned int nCases = 0;scanf("%d",&nCases);
    for(unsigned int iCases = 1;iCases <= nCases;++iCases)
    {
        unsigned int r = 0,g = 0,b = 0,k = 0;scanf("%d%d%d%d",&r,&g,&b,&k);
        unsigned int minv = r,maxv = r,midv = r;
        if(g > maxv) maxv = g;
        if(b > maxv) maxv = b;
        if(g < minv) minv = g;
        if(b < minv) minv = b;
        assert(k <= maxv);
        midv = (r+g+b-minv-maxv);

        unsigned int ans = 0;
        if(k <= minv) ans = k*3 - 2;
        else if(k <= midv) ans = minv + 2*k - 1;
        else ans = minv + midv + k;
        printf("%u\n",ans);
    }
    return 0;
}



CHEFPATH

(1)问题描述:Given an NxM maze, find out if there is a path starting at some cell (a,b), passing through all the cells of the maze and then ends in a cell (c,d) which is adjacent to the starting cell (i.e. they have a border in common). Along the path one can move from the current cell only in one of the 4 directions (up, down, left, right).

(2)要点:对于1*n特殊考虑。每次移动都会使得x坐标和y坐标的和的奇偶性变化,可以证明奇数*奇数是不可能有解的,而对于有一个边是偶数的,可以构造出其解:(0,0),(0,1)...(0,n-1),(1,n-1),(1,n-2)...(1,1),(2,1)(2,2)..(2,n)...(n-1,n-1),(n-1,n-2),...(n-1,1),(n-1,0),(n-2,0),...(1,0)。

(3)代码:

#include <stdio.h>
#include <assert.h>

int main()
{
    unsigned int nCases = 0;scanf("%d",&nCases);
    for(unsigned int iCases = 1;iCases <= nCases;++iCases)
    {
        unsigned long long n = 0,m = 0;scanf("%lld%lld",&n,&m);
        bool ans = false;
        if(1 == n || 1 == m)
        {
            if(2 == n || 2 == m) ans = true;
        }
        else
        {
            if(0 == (n&1)) ans = true;
            if(0 == (m&1)) ans = true;
        }
        printf("%s\n",ans?"Yes":"No");
    }
    return 0;
}


BIPIN3

(1)问题描述:A rooted tree with N nodes is considered. We need to assign a color from 1 to K to each node of the tree in such a way that every pair of nodes (A,B), where A is the parent of B, has different colors.

(2)要点:(k-1)^(n-1)*k

(3)代码:

#include <stdio.h>
#include <assert.h>
// 计算a^x (mod module)
static unsigned int quick_modexp(unsigned int a,unsigned int module,unsigned long long x);

int main()
{
	static const unsigned int module = 1000000007;
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0,k = 0;scanf("%d%d",&n,&k);
		unsigned long long ans = k;
		ans *= quick_modexp(k-1,module,n-1);
		ans %= module;
		printf("%u\n",(unsigned int)(ans));
	}
	return 0;
}



FIBQ

(1)问题描述:

(2)要点:

(3)代码:



(1)问题描述:

(2)要点:

(3)代码:



(1)问题描述:

(2)要点:

(3)代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值