CodeChef2015年9月问题


 MSTEP

(1)问题描述:给定1至n^2的数,每个数有且仅有一次地填入一个n*n的矩形当中。定义共用一条边的两个格子为相邻的格子,每一次移动只能移动到相邻的格子,问要按照1移动到2,再移动到3,再移动到4……最后移动到n^2,最少移动次数是多少?【Given is a grid of size N times N. It contains numbers from 1 to N^2. You start at the cell numbered 1, move to 2, then to 3, and so on up till N^2. Report the total manhattan distance traversed in doing so.】

(2)问题要点:Cakewalk

(3)代码:

#include <stdio.h>
#include <vector>
using std::vector;

int main()
{
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0,v = 0;scanf("%d",&n);
		vector<size_t> xpos(n*n,0),ypos(n*n,0);
		for(unsigned int i = 0;i < n;++i)
		{
			for(unsigned int k = 0;k < n;++k)
			{
				scanf("%d",&v);--v;
				xpos[v] = i;ypos[v] = k;
			}
		}
		unsigned long long ans = 0;
		for(unsigned int i = 1;i < n*n;++i)
		{
			int xdis = xpos[i] - xpos[i-1];
			int ydis = ypos[i] - ypos[i-1];
			if(xdis < 0) xdis = 0 - xdis;
			if(ydis < 0) ydis = 0 - ydis;
			ans += xdis + ydis;
		}
		printf("%llu\n",ans);
	}
	return 0;
}



DONUTS

(1)问题描述:Given chains of doughnuts of different lengths, we need to join them to form a single change. Two chains can be joined by cutting a doughnut into two and using it to clip the chains together. We need to minimize the number of cuts needed to form a single chain consisting of all the N doughnuts.

(2)问题要点:参考代码注释

(3)代码:

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <assert.h>
using std::vector;

/*
 添加一项0到数组中排序后,原问题等价于:求(k,x)使得(a[0]+...+a[k]+x)+1 = m-k(0<=k<=m,0<=x<a[k+1])
 即求(k,x)使得(a[0]+...+a[k]+x)+k = m-1,原问题的答案为m-k-1
 记sum[k] = a[0]+a[1]+...+a[k]+k,则等价于求(k,x)使得sum[k]+x=m-1
 由于x<a[k+1],所以sum[k]+x < sum[k]+a[k+1] < sum[k]+a[k+1]+1 = sum[k+1],即sum[k+1] > m-1 >= sum[k]
*/
int main()
{
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0,m = 0;scanf("%d%d",&n,&m);
		vector<unsigned int> data(m+1,0);
		for(unsigned int i = 1;i <= m;++i) scanf("%d",&data[i]);
		std::sort(data.begin(),data.end());
		vector<unsigned long long> sum(m+1,0);
		for(unsigned int i = 1;i <= m;++i) sum[i] = sum[i-1] + data[i];
		assert(sum[m] == n);
		for(unsigned int i = 1;i <= m;++i) sum[i] = sum[i] + i;
		vector<unsigned long long>::const_iterator itFind = std::upper_bound(sum.begin(),sum.end(),m-1);
		assert(itFind != sum.end() && *itFind >= m - 1);
		unsigned int ans = m - 1 - (itFind - sum.begin() - 1);
		printf("%u\n",ans);
	}
	return 0;
}


BANROB

(1)问题描述:Given that two thieves have 1 billion to divide amongst them according to a particular method, report the amounts both the thieves will receive upon division. The particular method here is that after a certain amount of time t, only (1 Billion * p^t) amount of money can be taken, where 0<=p<=1. Also, there are only M minutes to divide the stolen money and escape.

(2)要点:类似于“海盗分金”,分析可以知道,Chef最佳策略下分的为total*(1 - (-p)^m)/(1+p)

(3)代码:

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

int main()
{
	static const long double sum = 1000000000;
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned long long m = 0;
		long double p = 0,ans = 0;scanf("%lld%Lf",&m,&p);
		ans = sum*p/(1+p);
		long double v = pow((long double)(0-p),(int)(m-1));
		if(p <= 0.1 && m >= 20) v = 0;
		else if(p <= 0.9 && m >= 500) v = 0;
		ans *= (1-v);
		printf("%.4f %.4f\n",(double)(sum-ans),(double)(ans));
	}
	return 0;
}

LIGHTHSE


(1)问题描述:Given are the coordinates of N islands in cartesian plane. We are supposed to place lighthouses on some or all of these islands such that each one of them gets illuminated. A lighthouse can throw light only in one of the following four directions: North West, North East, South East and South West.

(2)要点:先判断minx,maxx,miny,maxy构成的4个点是否存在,如果存在则一个点即可。否则,按照(x,y)进行排序,则取最小点的的NE/SE和最大点的SW/NW必然可以覆盖,即最多不超过2个。根据最小点和最大点的y轴坐标的大小关系决定是NE+SW还是SE+NW。

(3)代码:

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <assert.h>
using std::vector;

typedef std::pair<int,int> pair_t;
typedef std::pair<pair_t,unsigned int> point_t;

unsigned int slove_one(const vector<point_t>& points,int x,int y)
{
	point_t p;
	p.first.first = x;
	p.first.second = y;
	p.second = 0;
	vector<point_t>::const_iterator itLo = std::lower_bound(points.begin(),points.end(),p);
	if(itLo == points.end()) return (unsigned int)(-1);
	if(itLo->first == p.first) return itLo->second;
	return (unsigned int)(-1);
}

int main()
{
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0;scanf("%d",&n);
		int minx = 2000000000,maxx = -2000000000,miny = 2000000000,maxy = -2000000000,x = 0,y = 0;
		vector<point_t> points(n);
		for(unsigned int i = 0;i < n;++i)
		{
			scanf("%d%d",&x,&y);
			if(x < minx) minx = x;
			if(x > maxx) maxx = x;
			if(y < miny) miny = y;
			if(y > maxy) maxy = y;
			points[i].first.first = x;
			points[i].first.second = y;
			points[i].second = i + 1;
		}

		std::sort(points.begin(),points.end());
		assert(points[0].first.first == minx && points[n-1].first.first == maxx);

		unsigned int p1 = slove_one(points,minx,miny);
		unsigned int p2 = slove_one(points,minx,maxy);
		unsigned int p3 = slove_one(points,maxx,miny);
		unsigned int p4 = slove_one(points,maxx,maxy);

		if(p1 != (unsigned int)(-1))
		{
			printf("1\n%u NE\n",p1);
		}
		else if(p2 != (unsigned int)(-1))
		{
			printf("1\n%u SE\n",p2);
		}
		else if(p3 != (unsigned int)(-1))
		{
			printf("1\n%u NW\n",p3);
		}
		else if(p4 != (unsigned int)(-1))
		{
			printf("1\n%u SW\n",p4);
		}
		else
		{
			if(points[0].first.second < points[n-1].first.second)
			{
				printf("2\n%u NE\n%u SW\n",points[0].second,points[n-1].second);
			}
			else
			{
				printf("2\n%u SE\n%u NW\n",points[0].second,points[n-1].second);
			}
		}
	}
	return 0;
}



(1)问题描述:Given two sequences a and b and a recurrence to calculate a_i and b_i, we need to calculate a certain value c when a pair of corresponding terms of a and b are given.

(2)要点:

(3)特别标注:题目对于i,k,s的范围描述是有问题的,采用long long方可正确。

(4)代码:

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

int main()
{
	static const unsigned int maxexp = 63;
	unsigned long long exp2[maxexp+1] = { 1 };
	for(unsigned int ie = 1;ie <= maxexp;++ie) exp2[ie] = exp2[ie-1]*2;

	static const double sqrt2 = 1.414213562373;
	static const double sqrt3 = 1.732050807569;
	static const double sqrt6 = 2.449489742783;
	//

	long long s = 0;
	unsigned long long i = 0,k = 0,ai = 0,bi = 0;
	scanf("%lld%lld%lld%lld%lld",&i,&k,&s,&ai,&bi);
	//scanf("%d%d%I64d%d%d",&i,&k,&s,&ai,&bi);
	long double ans = 0;
	long long expv = 0;
	if(i == k)
	{
		ans = ai + bi;
	}
	else if(i < k)
	{
		long long p = (k-i)/2,r = (k-i)&1;

		long double air = ai,bir = bi;
		if(1 == r) { air *= sqrt2;bir *= sqrt6; }
		ans = air + bir;

		if(1 == r) expv = 4*p + 1;
		else expv = 4*p;
	}
	else
	{
		long long p = (i-k)/2,r = (i-k)&1;

		long double air = ai,bir = bi;
		if(1 == r) { air *= sqrt2;bir *= sqrt6; }
		ans = air + bir;

		if(1 == r) expv = - 4*p - 3;
		else expv = 0 - 4*p;
	}

	expv -= s;

	if(expv >= 0)
	{
		assert(expv <= maxexp || 0 == ans);
		if(0 == ai && 0 == bi) ans = 0;
		else if(expv <= maxexp) ans *= exp2[expv];
		else ans = 0;
	}
	else
	{
		expv = 0 - expv;
		if(expv >= maxexp) ans = 0;
		else ans /= exp2[expv];
	}

	printf("%.6f\n",(double)(ans));
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值