TZOJ 5101 A Game(区间DP)

描述

Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end of the gameboar. That number is then deleted from the board, and its value is added to the score of the player who selected it. A player wins if his sum is greater than his opponents.

Write a program that implements the optimal strategy. The optimal strategy yields maximum points when playing against the "best possible" opponent. Your program must further implement an optimal strategy for player 2.

输入

Line 1:    N, the size of the board    

Line 2-etc:    N integers in the range (1..200) that are the contents of the game board, from left to right   

输出

Two space-separated integers on a line: the score of Player 1 followed by the score of Player 2.

样例输入

6
4 7 2 9
5 2

样例输出

18 11

题意

1*N的游戏盘,每个格子都有价值,玩家一次拿最左或最右,拿了删掉这个格子,问玩家1先拿,玩家2后拿,问俩人都最优可以拿多少分

题解

dp[i][j]=区间[i,j]先手-后手的差值

很容易推出dp[i][j]可由小区间得到

dp[i][j]=a[i]-dp[i+1][j];

dp[i][j]=a[j]-dp[i][j-1];

最终我们得到dp[1][n]为先手-后手的差值

设玩家1拿了V1,玩家2拿了V2

V1+V2=Σa;

V1-V2=dp[1][n];

求得

V1=(Σa+dp[1][n])/2;

V2=(Σa-dp[1][n])/2;

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int n;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         int a[105],dp[105][105]={0},sum=0;
10         for(int i=1;i<=n;i++)
11             scanf("%d",&a[i]),sum+=a[i];
12         for(int len=1;len<=n;len++)
13             for(int i=1;i<=n-len+1;i++)
14             {
15                 int j=i+len-1;
16                 dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]);
17             }
18         printf("%d %d\n",(sum+dp[1][n])/2,(sum-dp[1][n])/2);
19     }
20     return 0;
21 }

转载于:https://www.cnblogs.com/taozi1115402474/p/10269781.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395450.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

国家职业标准职业编码查询_为什么我学会编码而不是从事金融职业

国家职业标准职业编码查询by Amir Ghafouri通过阿米尔加富里(Amir Ghafouri) 为什么我学会编码而不是从事金融职业 (Why I learned to code instead of pursuing a career in finance) Last year I faced a major life and career decision: commit to pursuing a Chartered F…

go tool trace goalng调优工具

为什么80%的码农都做不了架构师&#xff1f;>>> 你想知道你的Go程序在做什么吗&#xff1f; go tool trace 可以向你揭示&#xff1a;Go程序运行中的所有的运行时事件。 这种工具是Go生态系统中用于诊断性能问题时&#xff08;如延迟&#xff0c;并行化和竞争异常…

程序员 文本编辑器 c语言,程序员必备的五款文本编辑器

原标题&#xff1a;程序员必备的五款文本编辑器程序员的工作离不开文本编辑器&#xff0c;有人说一个txt就能搞定&#xff0c;但txt面对如今复杂的要求&#xff0c;明显有些捉襟见肘&#xff0c;下面推荐五款超级好用的文本编辑器及搭配软件&#xff0c;绝对是程序员的大爱。程…

PCH文件的创建和配置

1.PCH文件的的创建 (1)CommandN (2)打开新建文件窗口:ios->other->PCH file&#xff0c;创建一个pch文件 2.PCH文件的配置 (1)在工程的TARGETS里边Building Setting中搜索Prefix Header (2)然后在Precompile Prefix Header下边的Prefix Header右边双击&#xff0c;添加刚…

ci 数据库异常捕获_系统地捕获错误:如何通过4个步骤构建GitLab CI测试管道

ci 数据库异常捕获by Joyz通过乔伊斯 系统地捕获错误&#xff1a;如何通过4个步骤构建GitLab CI测试管道 (Catch bugs systematically: how to build a GitLab CI testing pipeline in 4 steps) Your first app is a hit the day it’s launched. But one week later, you rea…

(小白)函数一: 声明函数的方法—语句定义法和表达式定义法的区别

一、函数的定义&#xff1a; 在说明什么是函数前先举一个小例子&#xff1a; 大家都知道印刷术是我国的四大发明&#xff08;科普一下&#xff1a;中国四大发明&#xff1a;造纸术、印刷术、火药、指南针&#xff09;之一&#xff0c;之所以有印刷术&#xff0c;是因为重复的抄…

android限制输入字符的范围,Android EditText 对输入字数和内容范围进行限制

在做定制机时&#xff0c;对光敏值进行范围控制时&#xff0c;以及对区号输入时遇到对输入字数以及输入内容的显示。找了好多方法&#xff0c;终于找到了几种方法其中EditText的addTextChangedListener功不可没。例如对光敏值要在0到61之间。大于61时要在输入框中自动变为61.代…

vue13过滤器 debounce延迟、limitBy、filterBy、orderBy

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>智能社——http://www.zhinengshe.com</title><meta name"viewport" content"widthdevice-width, initial-scale1.0, maximum…

Sass:一种CSS预处理器语言

http://sass-lang.com/ Sass是一种CSS预处理器语言&#xff0c;通过编程方式生成CSS代码。因为可编程&#xff0c;所以操控灵活性自由度高&#xff0c;方便实现一些直接编写CSS代码较困难的代码。 同时&#xff0c;因为Sass是生成CSS的语言&#xff0c;所以写出来的Sass文件是不…

Python学习(五)列表的简单操作

#!/usr/bin/env python#_*_coding:utf8_*_# 操作列表# for循环nbaStars [yaoming,kobe,manu,23,the klaw]for nbaStar in nbaStars: print(nbaStar)nbaStars [yaoming,kobe,manu,str(23),the klaw] # 这里有 int 对象&#xff0c;没有title方法的for nbaStar in nbaStars:…

node seneca_使用Node.js和Seneca编写国际象棋微服务,第3部分

node senecaFinishing up a three-part series on writing a rules engine with Seneca microservices.完成有关使用Seneca微服务编写规则引擎的三部分系列文章。 Parts 1 & 2 of this series covered:本系列的第1部分和第2部分涉及&#xff1a; The Seneca microservice…

Android开发画布销毁,Android DialogFragment 在页面销毁下的使用方式

今天看到了一篇文章,讲了DialogFragment的封装方式(Android&#xff1a;我为何要封装DialogFragment&#xff1f;),想到当初也为页面销毁后DialogFragment的回调方式头疼了好久,看到了po主的思路,与当初自己想的不太一样,就整理一下.如何在开发中遇到页面销毁的情况在android开…

视觉智能产品发布 阿里云这项世界第一的技术现在人人可用

用手机拍下朋友的相片&#xff0c;软件会自动识别进行分类并将照片发送给朋友。这不是空想&#xff0c;利用视觉智能对手机相册进行管理、分类和分享正逐步成为现实。在6月10日举行的云栖大会上海峰会上&#xff0c;阿里云正式发布了“图像识别”和“人脸识别”两款视觉智能服务…

ViewPager中Fragment的重复创建、复用问题

在ViewPager中的Fragment的生命周期 随着页面的切换 当前的展示页相邻的页面生命周期一直在变化 一开始 刚进入Activity时候&#xff0c;ViewPager默认初始化好前两个Fragment&#xff08;消息和任务&#xff09; 消息 ->任务 05-09 14:47:39.593 31509-31509/tyh.com.tabl…

使用VB.net建立excel文件

Add the following code snippet on top of Form1.vb Imports Excel Microsoft.Office.Interop.Excel Public Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickDim appXL As Excel.Application 声明一个application对象Dim wbX…

沙盘演练工作坊-产品开发_Google认证的Android助理开发人员:考试演练

沙盘演练工作坊-产品开发by Rohan Taneja由Rohan Taneja Google认证的Android助理开发人员&#xff1a;考试演练 (Google Certified Associate Android Developer: Exam Walkthrough) UPDATE (24th July, 2018)更新(2018年7月24日) The certification exam is available agai…

linux hlist,linux内核V2.6.11学习笔记(2)--list和hlist

这两个数据结构在内核中随处可见,不得不拿出来单独讲讲.这两个数据结构都是为了方便内核开发者在使用到类似数据结构的时候不必自行开发(虽然不难),因此它们需要做到足够的"通用性",也就是说,今天可以用它们做一个存放进程的链表,明天同样可以做一个封装定时器的链表…

14-angular.isDefined

判断括号内的值是否存在。 格式&#xff1a; angular.isDefined(value); value: 被判断是否存在的值。 返回值&#xff1a; true/false转载于:https://www.cnblogs.com/ms-grf/p/6978886.html

实施工程师1分钟即时演讲_我是如何在1年内从时装模特转变为软件工程师的

实施工程师1分钟即时演讲In 2015 I knew almost nothing about coding. Today, I’m a software engineer and a teacher at a code school for kids.在2015年&#xff0c;我对编码几乎一无所知。 今天&#xff0c;我是一名软件工程师&#xff0c;还是一所代码学校的儿童老师。…

MSSQL分组取后每一组的最新一条记录

数据库中二张表&#xff0c;用户表和奖金记录表&#xff0c;奖金记录表中一个用户有多条信息&#xff0c;有一个生效时间&#xff0c;现在要查询&#xff1a; 奖金生效时间在三天前&#xff0c;每个用户取最新一条奖金记录&#xff0c;且用户末锁定 以前用的方法是直接写在C#代…