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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章