POJ 2738 Two Ends 记忆化搜索

题意比较简单,两个人拿偶数张卡片,要么拿当前的最左边,要么拿最右边,尽量拿大的,拿完后求第一个人拿的卡片数字总和减去第二个人卡片的数字总和,求最大差值。

刚开始写记忆化搜索,参考了一下别人的代码,但是还有一个地方不太明白,就是当第一个人拿右边的时候,为什么写成num[x]<=num[y-1]会wa(⊙o⊙)…

求指教额。。。

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#define N 1100
using namespace std;
int num[N];
int dp[N][N];
int dfs(int x,int y)
{
    int a,b;
    if(dp[x][y]!=-1)
    return dp[x][y];

    if(x==y-1)
        return dp[x][y]=abs(num[x]-num[y]);

     if(num[x+1]>=num[y])//如果第一个人选左边,则比较左边第二个和最后一个大小,大的则为第二个人的选择,求出差值
         a=dfs(x+2,y)+num[x]-num[x+1];
         else
         a=dfs(x+1,y-1)+num[x]-num[y];

         if(num[x]<num[y-1])//同理,如果第一个人选择右边
         b=dfs(x,y-2)+num[y]-num[y-1];
         else
         b=dfs(x+1,y-1)+num[y]-num[x];

    return dp[x][y]=max(a,b);
}

int main()
{
    int n;
    int ca=1;
    while(~scanf("%d",&n))
    {
        if(n==0)  break;
        for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
        memset(dp,-1,sizeof dp);

        printf("In game %d, the greedy strategy might lose by as many as %d points.\n",ca++,dfs(1,n));
    }
    return 0;
}

POJ 2738 Two Ends 记忆化搜索

时间: 2024-10-09 09:24:29

POJ 2738 Two Ends 记忆化搜索的相关文章

POJ 2738 Two Ends(记忆化)

Description In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile.

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

poj 1695 Magazine Delivery 记忆化搜索

dp[a][b][c],表示三个人从小到大依次在a,b,c位置时,距离结束最少的时间. 每次选一个人走到c+1位置搜索就好了. 坑点在于不能floyd,估计题目没说清楚,意思就是如果没送Li,那么Li~n的点连去都不能去. #include<cstdio> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define INF 0x3f3f3f3f int

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

POJ 3176-Cow Bowling(DP||记忆化搜索)

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14210   Accepted: 9432 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard

[ACM] POJ 1088 滑雪 (记忆化搜索复习)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 77763   Accepted: 28905 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

poj 1088 滑雪【记忆化搜索】

题目链接:http://poj.org/problem?id=1088 基础题,不讲了,饿了 吃早饭去... 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #include <map> #include <string>

poj 2111 Millenium Leapcow(记忆化搜索)

Description The cows have revised their game of leapcow. They now play in the middle of a huge pasture upon which they have marked a grid that bears a remarkable resemblance to a chessboard of N rows and N columns (3 <= N <= 365). Here's how they se

poj 1085 Triangle War (状压+记忆化搜索)

Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Description Triangle War is a two-player game played on the following triangular grid: Two players, A and B, take turns filling in any dotted line connect