(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
#include
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 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
#include
#include
#include
using std::vector;
/*
添加一项0到数组中排序后,原问题等价于:求(k,x)使得(a[0]+...+a[k]+x)+1 = m-k(0<=k<=m,0<=x 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 data(m+1,0);
for(unsigned int i = 1;i <= m;++i) scanf("%d",&data[i]);
std::sort(data.begin(),data.end());
vector 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::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
#include
#include
#include
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
#include
#include
#include
#include
using std::vector;
typedef std::pair pair_t;
typedef std::pair point_t;
unsigned int slove_one(const vector& points,int x,int y)
{
point_t p;
p.first.first = x;
p.first.second = y;
p.second = 0;
vector::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 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;
}
#include
#include
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;
}