1. 程式人生 > >topcoder SRM 680 T3 Friendly Robot(DP)

topcoder SRM 680 T3 Friendly Robot(DP)

Problem Statement

You are programming a robot which lives on an infinite grid of empty cells. Each cell can be described by two integer coordinates (x, y). The robot always occupies a single cell of the grid. Initially, the robot is at coordinates (0, 0). A program has already been uploaded into the robot. The program is a sequence of instructions. Each instruction is one of the letters U, D, L, or R. These letters correspond to the commands ‘move up’, ‘move down’, ‘move left’, and ‘move right’ respectively. Once the robot is turned on, it will execute each instruction exactly once, in the given order. After executing all of the instructions, it will stop moving and it will turn itself off. You are given the robot’s current program in the string instructions. Whenever the robot returns to the cell (0, 0), it claps its hands. You find that funny, so you want to maximize the number of times the robot returns to the cell (0, 0). You are allowed to make at most changesAllowed modifications to the robot’s current program. Each modification consists of selecting a single character and changing it to a different character. (The new character still needs to be one of the letters U, D, L, or R. Note that you are not allowed to add or remove characters, you can only change the existing ones.) Please find and return the maximum number of times the robot can return to the cell (0, 0).
Definition

Class:
FriendlyRobot
Method:
findMaximumReturns
Parameters:
string, int
Returns:
int
Method signature:
int findMaximumReturns(string instructions, int changesAllowed)
(be sure your method is public)
Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

instructions will contain between 2 and 300 characters, inclusive.

Each character of instructions will be U, D, L, or R.

changesAllowed will be between 0 and the length of instructions, inclusive.
Examples
0)

“UULRRLLL”
1
Returns: 3
By changing the the first U to a D, you can make the robot return to its starting location 3 times. (Changing the second U to a D is also a valid solution.)
1)

“ULDR”
0
Returns: 1

2)

“ULDR”
2
Returns: 2

3)

“ULDRRLRUDUDLURLUDRUDL”
4
Returns: 8

4)

“LRLDRURDRDUDDDDRLLRUUDURURDRRDRULRDLLDDDDRLRRLLRRDDLRURLRULLLLLRRRDULRULULRLRDLLDDLLRDLUUDUURRULUDUDURULULLDRUDUUURRRURUULRLDLRRRDLLDLRDUULUURUDRURRLURLDLDDUUURRURRLRDLDDULLUDLUDULRDLDUURLUUUURRLRURRDLRRLLLRDRDUUUDRRRDLDRRUUDUDDUDDRLUDDULRURRDRUDLDLLLDLUDDRLURLDUDRUDDDDURLUUUDRLURDDDDLDDRDLUDDLDLURR”
47
Returns: 94

5)

“UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU”
300
Returns: 150

6)

“UD”
1
Returns: 1
Remember that you can change fewer than changesAllowed characters if you wish.

題意:

有一個機器人,從原點開始按照給定的操作移動,每次操作可以向上下左右中一個方向移動一格,現在可以改變k個操作,求機器人最多能經過原點幾次。

思路:

1.第一次想到的是把左移看做-1,右移+1,下移-400,上移+400,因為總步數不超過400,可以保證一個值表示一段確定的移動路線。然後就將問題轉化成求這個序列中修改k個值最多有多少個字首和為0。然而後面我做不出來了

2.接下來想到DP,f[i][j] 表示前i步,改變了j次,回到了原點,最多有幾次經過原點。注意回到了原點是很重要的條件,為了後面更新時不出錯。也可以說i為奇數的根本不用考慮,因為無論如何修改都不會走回原點。最開始寫DP的時候沒有考慮到f[i][j]走回原點這個條件,導致錯誤。具體操作:先預處理出不修改,走了前i步到的座標x[i]和y[i],轉移方程為f [ i ] [ k ] = max ( f [ i ] [ k ] , f [ j ] [ k - ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) / 2 ) ] + 1 ) ( j < i && k <= changeAllowed && ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) %2 == 0 )

貼個程式碼,紀念一下我想了好久,調了半小時,不得不回寢室睡覺,又想了一晚上,早上起床才A掉的題目。然而jack表示這種題不是一下就A掉了嗎

#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int n, x[N], y[N];
int f[N][N];

class FriendlyRobot {
public:
    int findMaximumReturns( string instructions, int changesAllowed );
};
int FriendlyRobot::findMaximumReturns(string instructions, int changesAllowed) {
    n = instructions.size();
    x[0] = y[0] = 0;
    for (int i = 1; i <= n; i++){
        x[i] = x[i-1];
        y[i] = y[i-1];
        if (instructions[i-1] == 'U') x[i]++;
        else if (instructions[i-1] == 'D') x[i]--;
        else if (instructions[i-1] == 'L') y[i]--;
        else y[i]++;
    }
    int ans = 0;
    memset(f, -1, sizeof(f));
    f[0][0] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < i; j++){
            int ope = abs(x[i]-x[j])+abs(y[i]-y[j]);
            if (ope&1) continue;
            else ope >>= 1;
            for (int k = ope; k <= changesAllowed; k++){
                if (f[j][k-ope] == -1) continue;
                f[i][k] = max(f[i][k], f[j][k-ope]+1);
                ans = max(ans, f[i][k]);
            }
        }
    return ans;
}

相關推薦

topcoder SRM 680 T3 Friendly RobotDP

Problem Statement You are programming a robot which lives on an infinite grid of empty cells. Each cell can be described by two in

Topcoder SRM 722 Div1 600Pts DominoTiling簡單插頭DP

lap 就是 ont 要求 net esp $1 true mes 題意 給定一個$12*12$的矩陣,每個元素是‘.‘或‘X‘。現在要求$1*2$的骨牌鋪滿整個矩陣, ‘X‘處不能放置骨牌。求方案數。 這道題其實和 Uva11270 是差不多

Topcoder SRM 301 Div2-1000 CorrectingParenthesization區間DP

完全 errors 實現 括號 cor ren opc fin 區間dp 題意 給定一個長度為偶數的字符串。這個字符串由三種括號組成。    現在要把這個字符串修改為一個符合括號完全匹配的字符串,改變一個括號的代價為$1$,求最小總代價。 區間DP。令$dp[i

Topcoder SRM 701 Div2-900 ThueMorseGame博弈+預處理

n) const ons ret 一個人 || spa 進制 fin 題意 Alice和Bob在玩一個遊戲,Alice先手。 每次一個人可以從一堆式子中拿走任意數量(不超過m)的式子。 取走最後一顆式子的人勝利。 當一個取完某一步的時候剩下的石子數量的二進制表示中1

Topcoder SRM 675 Div1 500Pts LimitedMemorySeries1分塊

tor bits fin get pre n) ted 多少 top 題意 給定一個長度不超過$5*10^{6}$的數列和不超過$100$個詢問,每次詢問這個數列第$k$小的數,返回所有詢問的和    內存限制很小,小到不能存下這個數列。(數列以種子的形式給出)   

POJ 1260-PearlsDP

ctype set lowest cas str pri font mount scan Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7465 Accepted

hdu5375 Gray codeDP

style 就會 gray code tar har sof case ref pro 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 題目大意:給你一個二進制串,帶’?’的位置能夠由你來決定填’1’還是’

[luoguP2858] [USACO06FEB]奶牛零食Treats for the CowsDP

turn pri class 分享 blank .org splay == pla 傳送門 f[i][j][k] 表示 左右兩段取到 i .... j 時,取 k 次的最優解 可以優化 k 其實等於 n - j + i 則 f[i][j] = max(f[i +

[luoguP2896] [USACO08FEB]一起吃飯Eating TogetherDP

onclick ati digi 次數 代碼 ide log lose 吃飯 傳送門 由於 Di 只有 3 種情況,那麽就很簡單了 f[i][j][0] 表示前 i 個,且第 i 個變成 j 的 遞增序列最小修改次數 f[i][j][1] 表示前 i 個,

[luoguP3052] [USACO12MAR]摩天大樓裏的奶牛Cows in a SkyscraperDP

摩天大樓 close 技術 printf opera col cli 裏的 pen 傳送門 輸出被閹割了。 只輸出最少分的組數即可。 f 數組為結構體 f[S].cnt 表示集合 S 最少的分組數 f[S].v  表示集合 S 最少分組數下當前組所用的最少容

[luoguP2915] [USACO08NOV]奶牛混合起來Mixed Up CowsDP

代碼 target pid bsp 傳送門 混合 http getc view 傳送門 f[i][S] 表示當前集合為 S,最後一個數為 i 的最優解 f[i][S] += f[j][S - i] (j, i ∈ S && j != i

[UVALive 7143]Room AssignmentDp

scan -s ping cst ins ron cstring ans ted Description There are N guests checking in at the front desk of the hotel. 2K (0 ≤ 2K ≤ N

[Codeforces Round #261 (Div. 2) E]Pashmak and GraphDp

solution and other main ems scanf homework max urn Description Pashmak‘s homework is a problem about graphs. Although he always tries

[luoguP1879] [USACO06NOV]玉米田Corn FieldsDP

lan void inline cor oid onclick https num open 傳送門 說要統計方案,感覺就是個 Σ 而矩陣中只有 01 ,可以用二進制表示 這樣,預處理出每一個每一行所有可能的狀態 s 然後初始化第一行所有狀態的方案

[luoguP2890] [USACO07OPEN]便宜的回文Cheapest PalindromeDP

span char play click aps cst 代碼 include sdi 傳送門 f[i][j] 表示區間 i 到 j 變為回文串所需最小費用 1.s[i] == s[j]  f[i][j] = f[i + 1][j - 1] 2.s[i] !=

HIT2244 Get the Colorsdp

ron strong newest 思路 bmi 計算 per sin queue 題目鏈接:   http://acm.hit.edu.cn/hoj/problem/view?id=2244 題目描述: Get the Colors Submitted : 5

dpCodeForces - 300D Painting Square

clas 預處理 action 技術 sign std ces span ecif Vasily the bear has got a large square white table of n rows and n columns. The table has got a

[luoguP1373] 小a和uim之大逃離DP

htm line target light eve str tdi bsp for 傳送門 題解 代碼 #include <cstdio> #include <iostream> #define N 802 #define

dpopenjudge 復雜的整數劃分問題

con fin can == names 劃分數 algorithm 系列 問題 將正整數n 表示成一系列正整數之和,n=n1+n2+…+nk, 其中n1>=n2>=…>=nk>=1 ,k>=1 。正整數n 的這種表示稱為正整數n 的劃分。

poj - 1088 - 滑雪dp

target art dsm 題目 ipp 每次 元素 org mod 題意:一個R * C的矩陣(1 <= R,C <= 100),元素代表該點的高度h(0<=h<=10000),從隨意點出發,每次僅僅能走上、下、左、右。且將要到的高度要比