提纲提纲提纲提纲提纲提纲提纲提纲提纲提纲提纲 GRE ,我快疯了,倒计时21天!加油!!!!!!!
摘要:
#include<iostream>#include<algorithm>#include<vector>using namespace std;char str[100][100];int v[100][100];int n,m;int vv[100...
阅读全文
cheer up,my friend,no body will gain without paying ,do as best as you can ,this is the answer.
Maybe this semester ,You have a lots of things to do , It is doesn't matter what the circumstance is.Insist yourself , and stand up.
一)巴什博弈(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取光者得胜。
很容易想到当n%(m+1)<>0时,先取必胜,第一次先拿走n%(m+1),以后每个回合到保持两人拿走的物品总和为m+1即可。
这个游戏还可以有一种变相的玩法:两个人轮流报数,每次至少报一个,最多报十个,谁能报到100者胜。
(二)威佐夫博弈(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜。
如果甲面对(0,0),那么甲已经输了,这种局势我们称为奇异局势。前几个奇异局势是:(0,0)、(1,2)、(3,5)、(4,7)、(6,10).可以看出,a0=b0=0,ak是未在前面出现过的最小自然数,而 bk=ak+k.
那么任给一个局势(a,b),怎样判断它是不是奇异局势呢?我们有如下公式:
ak =[k(1+√5)/2],bk= ak + k (k=0,1,2,...,n 方括号表示取整函数)
奇妙的是其中出现了黄金分割数(1+√5)/2 = 1。618...,因此,由ak,bk组成的矩形近似为黄金矩形,由于2/(1+√5)=(√5-1)/2,可以先求出j=[a(√5-1)/2],若a=[j(1+√5)/2],那么a = aj,bj = aj + j,若不等于,那么a = aj+1,bj+1 = aj+1+ j + 1,若都不是,那么就不是奇异局势。然后再按照上述法则进行,一定会遇到奇异局势。
(三)尼姆博弈(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜。
对于任何奇异局势(a,b,c),都有a^b^c=0.
非奇异局势(a,b,c)(a<b<c)转换为奇异局势,只需将c变为a^b,即从c中减去 c-(a^b)即可。
这题其实和普通的DP题没什么区别,只是需要用dp[i]表示i状态下能够达到的最大值,而且由于有负数,需要将坐标轴整体平移一下。
在一进一步说,还是要求出所有的状态,类似穷举了。我想了下,如果真的要优化,可以只将出现过的状态记下来,这样扫描的时候可以剪去一些时间,也许0MS就是这么做的吧^_^ ,复杂度O(10^7)。
代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 999999999
int dp[400001];
int n;
int l,r;
#define OFFSET 200000
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
fill(dp,dp+400001,-INF);
dp[OFFSET]=0;
l=r=OFFSET;
scanf("%d",&n);
int i,j;
int a,b;
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d",&a,&b);
if(a<=0&&b<=0)
continue;
if(a<0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int k;
for(k=l;k<=r;k++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k]!=-INF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(k+a<l)
l=k+a;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k+a]==-INF)
dp[k+a]=dp[k]+b;
else
dp[k+a]=max(dp[k+a],dp[k]+b);
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int k;
for(k=r;k>=l;k--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k]!=-INF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(k+a>r)
r=k+a;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k+a]==-INF)
dp[k+a]=dp[k]+b;
else
dp[k+a]=max(dp[k+a],dp[k]+b);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
int res=0;
int k;
for(k=OFFSET;k<=OFFSET+1000*n;k++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k]>0&&k-OFFSET+dp[k]>res)
res=k-OFFSET+dp[k];
}
printf("%d\n",res);
return 0;
}
重温了下 背包九讲 不仅发现了其中的一些错误 也发现了这篇文章的局限性,例如这个题。。。做完之后 我对背包的概念又模糊了。。。
#include<iostream>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int dp[10001];
int w[10001];
int v[10001];
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int t;
int e,f;
int n;
int ww;
scanf("%d",&t);
int i,j,k;
for(i=1;i<=t;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d",&e,&f);
ww=f-e;
scanf("%d",&n);
for(j=1;j<=n;j++)
scanf("%d%d",&v[j],&w[j]);
memset(dp,-1,sizeof(dp));
dp[0]=0;
for(j=1;j<=n;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(k=0;k<=ww-w[j];k++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k]!=-1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(dp[k+w[j]]==-1)
dp[k+w[j]]=dp[k]+v[j];
else
dp[k+w[j]]=min(dp[k+w[j]],dp[k]+v[j]);
}
}
}
if(dp[ww]==-1)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[ww]);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)