Problem D: 勤奋的涟漪2 dp + 求导

http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3

dp[i][state]表示处理了前i个,然后当前状态是state的时候的最小休息天数。

比如用1代表休息,2是训练,3是运动。

如果当前这天允许的状态只是训练。

那么dp[i][3] = inf(表示不可能)

休息是无论何时都可能的了。从dp[i - 1][1--3]个状态转过来, + 1即可

那个求导是-24

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>

const int maxn = 100 + 20;
int dp[maxn][4];
void work() {
    int n;
    scanf("%d", &n);
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) {
        int x;
        scanf("%d", &x);
        if (x == 0) {
            dp[i][1] = min(dp[i - 1][1], min(dp[i - 1][2], dp[i - 1][3])) + 1;
            dp[i][2] = dp[i][3] = inf;
        } else if (x == 1) {
            dp[i][2] = dp[i - 1][1];
            dp[i][2] = min(dp[i][2], dp[i - 1][3]);
            dp[i][1] = min(dp[i - 1][1], min(dp[i - 1][2], dp[i - 1][3])) + 1;
            dp[i][3] = inf;
        } else if (x == 2) {
            dp[i][2] = inf;
            dp[i][3] = min(dp[i - 1][1], dp[i - 1][2]);
            dp[i][1] = min(dp[i - 1][1], min(dp[i - 1][2], dp[i - 1][3])) + 1;
        } else if (x == 3) {
            dp[i][1] = min(dp[i - 1][1], min(dp[i - 1][2], dp[i - 1][3])) + 1;
            dp[i][2] = min(dp[i - 1][1], dp[i - 1][3]);
            dp[i][3] = min(dp[i - 1][1], dp[i - 1][2]);
        }
    }
    int ans = inf;
    for (int i = 1; i <= 3; ++i) {
        ans = min(ans, dp[n][i]);
    }
    cout << ans * (-24) << endl;
}
int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    int t;
    scanf("%d", &t);
    while (t--) work();
    return 0;
}

时间: 2024-12-19 18:45:26

Problem D: 勤奋的涟漪2 dp + 求导的相关文章

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

BZOJ 3407: [Usaco2009 Oct]Bessie&#39;s Weight Problem 贝茜的体重问题( dp )

01背包... ----------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define clr( x , c ) m

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

树形dp求树的重心

Balancing Act http://poj.org/problem?id=1655 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int M=50010; 8 vector<int> g[

poj 1655 树形dp求取树的重心

http://poj.org/problem?id=1655 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the large

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

POJ 2096 (dp求期望)

A - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-05-15) Description Ivan is fond of collecting. Unlike other people who collect post stamps, co

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno