Gym 100703G---Game of numbers(DP)

来源:http://www.cnblogs.com/chen9510/archive/2016/09/16/5876782.html
-Advertisement-
Play Games

題目鏈接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s a good game, — Princess said pensively. It was cle ...


題目鏈接

http://vjudge.net/contest/132391#problem/G

 

Description

standard input/output
Statements

— It' s a good game, — Princess said pensively. It was clear that she was thinking about something else.

— They like to play various games here in Castles Valley. And they invent ones themselves. Say, my friend Knight played with a princess a game some time ago, — Dragon thought it was a good idea o tell Princess about another game, if, perhaps, previous game was seemed no interesting for her.

Princess A. offered Knight to play a game of numbers. She puts down the number zero on a sheet of paper. Let us call this number acurrent result.

Further steps of princess A. and Knight are described below. She calls any positive integer and Knight says what she must do with this number: to add it to the current result or subtract it from the current result.

Princess A. performs the action and calculates a new value. This value becomes the new current result.

Princess A. wants that current result to be not less than zero and not greater than k at any time. The game finishes when an action makes the result out of the range or when a sequence of n numbers, which princess A. conceived, exhausts.

Knight managed to learn the sequence of n numbers that princess A. guessed, and now he wants the game to last as long as possible.

Your task is to compute maximum possible number of actions which Knight is able to perform during the game.

Input

The first line contains integers n and k (1 ≤ n ≤ 1000,  1 ≤ k ≤ 1000) — the size of sequence which princess A. conceived and an upper bound for a current result which must not be exceeded.

The second line contains n integers c1, c2, ..., cn (1 ≤ cj ≤ k) — the sequence which princess A. conceived.

Output

In the first line print integer d — maximum possible number of actions, which Knight is able to perform during the game.

Print d symbols "+" and "-" in the second line. Symbol at jth position specifies an action which is applied to jth number in the princess' sequence. If multiple answers exist, choose any of them.

Sample Input

Input
2 5
3 2
Output
2
++
Input
5 5
1 2 3 4 5
Output
4
++-+


題意:輸入n,k 然後輸入n個正整數(每個數小於等於k 大於0)從第一個數開始加上或者減去這個數,使得當前的算式值在0~k之間,求這個算式的最大長度,
並輸出這個算式的運算符;

思路:DP,定義dp[i][j]表示由前i個數能否得到j,能則dp[i][j]=1,否則為0;

代碼如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
int a[1005];
char s[1005];
bool dp[1005][1005];

int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[1][a[1]]=1;
        int i,j;
        for(i=2;i<=n;i++)
        {
            int f=0;
            for(j=0;j<=k;j++)
            {
                if(dp[i-1][j])
                {
                    if(j+a[i]<=k) { dp[i][j+a[i]]=1; f=1; }
                    if(j>=a[i])   { dp[i][j-a[i]]=1; f=1; }
                }
            }
            if(f==0) break;
        }
        printf("%d\n",i-1);

        s[1]='+'; s[i]='\0'; i--;
        for(j=0;j<=k;j++)
        if(dp[i][j]) break;

        for(;i>=2;i--)
        {
            if(j>=a[i]&&dp[i-1][j-a[i]]) { s[i]='+'; j=j-a[i];}
            else { s[i]='-'; j=j+a[i]; }
        }
        puts(s+1);
    }
    return 0;
}


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文版權歸博客園和作者吳雙共同所有,歡迎轉載,轉載和爬蟲請註明博客園蝸牛原文地址 http://www.cnblogs.com/tdws/p/5874212.html。 最近打算分享一系列.NET Core實用後臺架構,所以首先介紹EF Core。目前國內各大論壇,各位大牛的分享,是按照Micros ...
  • 1.python中有一些基本規則的特殊字元。 (1)#表示這後的字元為python註釋。 (2)\n標準的行分隔符。 (3)\繼續上一行。(也就是過長的語句可以使用反斜杠(\)分解成幾行) (4);將兩個語句連接在一行。 (5):將代碼的頭和體分開。(多個語句構成一個代碼塊(代碼組),像if,whi ...
  • 如:/application/uctenter/controller 如:/application/uctenter/controller/User.php class User{ //屬性 public userName; //駝峰命名首字母小寫 //私有屬性 private _userAge;  ...
  • Counter(計數器) 是一個字典的子類,存儲形式同樣為字典,其中存儲的鍵為字典的元素,值為元素出現的次數,在使用之前我們需要先導入文件 import collections 初始化一個計數器 most_common(self,n) 取出元素最多的前n項 sorted(c) 給計數器排序 ''.j ...
  • 由於Django是動態網站,所有每次請求均會去數據進行相應的操作,當程式訪問量大時,耗時必然會更加明顯,最簡單解決方式是使用:緩存,緩存將一個某個views的返回值保存至記憶體或者memcache中,5分鐘內再有人來訪問時,則不再去執行view中的操作,而是直接從記憶體或者Redis中之前緩存的內容拿到 ...
  • 利用django的Q()功能可以很好的展開搜索功能 假設我要做個這樣的搜索功能 那麼思路是怎麼樣的? 那我們就來看看代碼 前端的代碼: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title ...
  • 今天看到園友心白水撰寫的《簡單翻譯工具--必應字典第三方API使用方法》,感覺很不錯,所以用Python也寫了一個。源碼如下: 程式運行的結果如下: 請輸入英文單詞: python美音:[ 'paɪθɑn ] 英音:[ 'paɪθ(ə)n ] n. 蟒;蚺蛇Web 蟒蛇;巨蟒;派森 例句en: Se ...
  • 分類 分類可以模塊化方法的定義,可以用於向現有的類添加新的方法。 分類提供了一種簡單的方式,用他可以將類的定義模塊化到相關方法的組或分類中。它還提供了拓展現有類定義的簡便方式,並且不必訪問類的源代碼,也無需創建子類。 分類可以通過兩種方法來實現: 1.繼承自一個分類:可以通過將分類名稱括在類名稱之後 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...