您的位置:首页 > 其它

sicily 1176

2014-10-12 10:16 239 查看
// Problem#: 1176
// Submission#: 2994117
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<memory.h>
#include<stdio.h>

using namespace std;

long long int dy[1001][1001];
int c[1001];
long long int score(int a, int b) {
  long long int temp1,temp2;
  temp1 = 0;
  temp2 = 0;
  if (a == b) {
    return c[a];
  }
  if(dy[a][b] != -1) {
    return dy[a][b];
  }
  if(b - a == 1) {
    return dy[a][b] = c[a] > c[b]? c[a] - c[b]:c[b] - c[a];
  } else if ((b - a)%2 == 1) {
    temp1 = score(a,b-1) + c[b];
    temp2 = score(a+1,b) + c[a];
      return dy[a][b] = temp1>temp2? temp1:temp2;
  } else if ((b - a)%2 == 0) {
    if(c[a] >= c[b]) {
      return dy[a][b] = score(a+1,b) - c[a];
    } else {
      return dy[a][b] = score(a,b-1) - c[b];
    }
  }
}

int main() {
  int n;
  int time = 0;
  while(scanf("%d",&n) && n != 0) {
    memset(c, 0, sizeof (c));  
    memset(dy, -1, sizeof(dy));  
    time++;
    for (int i = 0; i < n; i++) {
      scanf("%d",&c[i]);
    }
    cout<<"In game "<< time <<", the greedy strategy might lose by as many as "<< score(0,n-1) <<" points."<<endl;   
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: