uva10891Game of sum

題意:經典的取石子游戲是這樣的:有一堆石子,A、B兩個人輪流取,每次取一顆,只能從邊上取,每個石子有相應的價值,A、B兩人都想使得自己的價值最多,兩個人足夠聰明,問最后價值分別是多少

本題則是可以取多顆,但仍然只能從一側取得

分析:狀態轉移方程

best[i][j]=sum[i][j]-min(best[i][j-k],best[i+k][j], 0);{1<=k=j-i+1}.

使用了記憶化的方法,O(n3),書上說有進一步的優化,不過當前數據下已經很快了(64ms)

代碼:

View Code
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 using namespace std;
 5 const int MAXN = 100 + 10;
 6 #define DEBUG
 7 int min(int a, int b){
 8     return a<b?a:b;
 9 }
10 int s[MAXN], a[MAXN], d[MAXN][MAXN], vis[MAXN][MAXN], n;
11 int dp(int i, int j){
12     if(vis[i][j]) return d[i][j];
13     vis[i][j]=1;
14     int m=0, k;
15     for(k=i+1; k<=j; k++) m=min(m, dp(k, j));
16     for(k=i; k<j; k++) m=min(m, dp(i, k));
17     d[i][j]=s[j]-s[i-1]-m;;
18     return d[i][j];
19 }
20 int main(){
21 #ifndef DEBUG
22     freopen("in.txt", "r", stdin);
23 #endif
24     while(scanf("%d", &n)!=EOF && n){
25         s[0]=0;
26         int i;
27         for(i=1; i<=n; i++){
28             scanf("%d", &a[i]);
29             s[i] = s[i-1] + a[i];
30         }
31         memset(vis, 0, sizeof(vis));
32         printf("%d\n", 2*dp(1,n)-s[n]);
33     }
34     return 0;
35 }

?

?

轉載于:https://www.cnblogs.com/zjutzz/archive/2013/02/14/2911230.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274361.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274361.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274361.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用戶體驗設計師能為seo做_用戶體驗設計師可以從產品設計歷史中學到什么

用戶體驗設計師能為seo做Many things have changed from tool design in the prehistoric era to today’s digital product design. However, we can see surprisingly many similarities. Especially when it comes down to one particular aspect: usability.從史前時代的工…

函數指針

顧名思義&#xff0c;指針函數即返回指針的函數。其一般定義形式如下&#xff1a; 類型名 *函數名(函數參數表列); 其中&#xff0c;后綴運算符括號“()”表示這是一個函數&#xff0c;其前綴運算符星號“*”表示此函數為指針型函數&#xff0c;其函數值為指針&#xff0c;即它…

orton效果_如何使圖片發光:Orton效果

orton效果Have you ever seen an impossibly dream-like landscape photo? One with a slow burning, glowing sunset. That’s really the best way to describe it, the image looks as if it’s glowing. You might be thinking, “wow, I wish I was that good and could …

UVA10785 The Mad Numerologist

雖然是sorting的壓軸&#xff0c;但是比起前面真心水題。這個專題結合前面string的很多&#xff0c;排序相對簡單了&#xff0c;qsort基本解決。 題目&#xff1a; The Mad Numerologist Numerology is a science that is used by many people to find out a mans personality,…

蘋果人機交互指南_蘋果人機界面設計指南的10個見解

蘋果人機交互指南重點 (Top highlight)I’ve been developing an IOS app for the past few months and have been constantly referring to Apple’s Human Interface Design Guidelines. I would consider it a must-read for any aspiring or current UI/UX designer.在過去…

也來學學插件式開發

上一家公司有用到插件式開發來做一個工具箱&#xff0c;類似于QQ電腦管家&#xff0c;有很多工具列表&#xff0c;點一下工具下載后就可以開始使用了。可惜在那家公司待的時候有點短&#xff0c;沒有好好研究一下。現在有空&#xff0c;自己在網上找了些資料&#xff0c;也來試…

同態加法_我對同態的想法

同態加法Early February, I uploaded this shot onto Dribbble. Nothing fancy –– just two screens experimenting with “2月初&#xff0c;我將這張照片上傳到Dribbble。 沒什么幻想–只有兩個屏幕在嘗試“ Neumorphism,” or soft UI. Little did I know that this post…

php內核探索

引自&#xff1a;http://www.nowamagic.net/librarys/veda/detail/1285 SAPI:Server Application Programming Interface 服務器端應用編程端口。研究過PHP架構的同學應該知道這個東東的重要性&#xff0c;它提供了一個接口&#xff0c;使得PHP可以和其他應用進行交互數據。 本…

hp-ux鎖定用戶密碼_UX設計101:用戶研究-入門需要了解的一切

hp-ux鎖定用戶密碼這是什么&#xff1f; (What is this?) This session is part of a learning curriculum that I designed to incrementally skill up and empower a team of Designers and Researchers whose skillset and ways of working needed to evolve to keep up wi…

等比數列前N項和的公式推導

設等比數列的前n項和為S(n), 等比數列的第一項為a1&#xff0c;比值為q。 &#xff08;1&#xff09;S(n) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1);&#xff08;2&#xff09;S(n1) a1 a1 * q a1 * q ^ 2 .... a1 * q ^ (n - 1) a1 * q ^ n;由&#xff08;2&am…

extjs6 引入ux_關于UX以及如何擺脫UX的6種常見誤解

extjs6 引入uxDo you ever browse social media, internet, or talk to colleagues and hear them say something UX related you disagree with so much that you just want to lecture them on the spot?您是否曾經瀏覽過社交媒體&#xff0c;互聯網或與同事交談&#xff0c…

Cocos2D-HTML5開源2D游戲引擎

http://www.programmer.com.cn/12198/ Cocos2D-HTML5是基于HTML5規范集的Cocos2D引擎的分支&#xff0c;于2012年5月發布。Cocos2D-HTML5的作者林順將在本文中介紹Cocos2D-HTML5的框架、API、跨平臺能力以及強大的性能。Cocos2D-HTML5是Cocos2D系列引擎隨著互聯網技術演進而產生…

illustrator下載_Illustrator筆工具練習

illustrator下載Adobe Illustrator is a fantastic vector creation tool and you can create a lot of things without ever using the Pen Tool. However, if you want to use Illustrator at its full potential, I personally believe that you need to master and become …

怎么更好練習數位板_如何設計更好的儀表板

怎么更好練習數位板重點 (Top highlight)Dashboard noun \?dash-?b?rd\ A screen on the front of a usually horse-drawn vehicle to intercept water, mud, or snow.儀表盤 名詞\ ?dash-?b?rd \\通常在馬拉的車輛前部的屏幕&#xff0c;用來攔截水&#xff0c;泥或雪。…

學習正則表達式

deerchao的blog Be and aware of who you are. 正則表達式30分鐘入門教程 來園子之前寫的一篇正則表達式教程&#xff0c;部分翻譯自codeproject的The 30 Minute Regex Tutorial。 由于評論里有過長的URL,所以本頁排版比較混亂,推薦你到原處查看,看完了如果有問題,再到這里來提…

人物肖像速寫_去哪兒? 優步肖像之旅

人物肖像速寫In early 2018, the Uber brand team started a rebranding exercise, exploring a fresh take on what it means to be a global transportation and technology company. A new logo was developed in tandem with a bespoke sans-serif typeface called Uber Mo…

js獲取和設置屬性

function square(num){ var total num*num;//局部變量 return total;}var total 50;//全局變量var number square(20);alert(total);//結果為50function square(num){ total num*num;//全局變量 return total;}var total 50;//全局變量var number square(20)…

hp-ux鎖定用戶密碼_我們如何簡化925移動應用程序的用戶入門— UX案例研究

hp-ux鎖定用戶密碼Prologue: While this is fundamentally a showcase of our process in the hopes of helping others, it’s also a story about the realism of limitations when working with clients and how we ultimately were able to deliver a product the client w…

微信公眾號無需二次登錄_您無需兩次解決問題-您需要一個設計系統

微信公眾號無需二次登錄重點 (Top highlight)The design system concept can be differently defined according to each person’s background. Designers may say that a design system is a style guide while developers may say it is UI standards, or specs, or even as…

android中AsyncTask和Handler對比

1 &#xff09; AsyncTask實現的原理,和適用的優缺點 AsyncTask,是android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實現異步操作,并提供接口反饋當前異步執行的程度(可以通過接口實現UI進度更新),最后反饋執行的結果給UI主線程. 使用的優點: l 簡單,快捷 l 過程可…