CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】

这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小

根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的

Because the total thickness of vertical books is fixed it‘s good to calculate the minimum possible total width of horizontal books. 

那么只需要模拟一遍放书的过程即可,不会TLE

 

不过正统解法是Dp

Dp Source Code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;
const int INF = 0x3f3f3f3f   ;

int dp[101][210][301];

int main(){
    std::ios::sync_with_stdio(false);
    int i, j, t, k, u, v, x, y, numCase = 0;
    int n, a, b;
    dp[0][0][0] = 1;
    cin >> n;
    int cur = 0;
    for(i = 0; i < n; ++i){
        cin >> a >> b;
        for(j = 0; j < cur + 1; ++j){
            for(k = 0; k < 201; ++k){
                dp[i + 1][j + a][k] |= dp[i][j][k];
                dp[i + 1][j][k + b] |= dp[i][j][k];
            }
        }
        cur += a;
    }
    for(i = 0; i < 201; ++i){
        for(j = 0; j < i + 1; ++j){
            if(dp[n][i][j]){
                cout << i << endl;
                return 0;
            }
        }
    }

    return 0;
}

My Source Code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;
const int INF = 0x3f3f3f3f   ;

int a[10][122];
int one, two, n;

int main(){
    std::ios::sync_with_stdio(false);
    int i, j, t, k, u, v, x, y, numCase = 0;
    cin >> n;
    for(u = 0; u < n; ++u){
        cin >> x >> y;
        if(x == 1){
            a[1][one++] = y;
        } else{
            a[2][two++] = y;
        }
    }
    sort(a[1], a[1] + one);
    sort(a[2], a[2] + two);
    int ans = INF;
    for(i = 0; i <= one; ++i){
        for(j = 0; j <= two; ++j){
            int cur = 0;
            for(k = 0; k < one - i; ++k){
                cur += a[1][k];
            }
            for(k = 0; k < two - j; ++k){
                cur += a[2][k];
            }
            if((i + 2 * j) < ans && (i + 2 * j) >= cur){
                ans = i + 2 * j;
            }
        }
    }
    cout << ans << endl;

    return 0;
}
时间: 2024-08-01 20:33:44

CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】的相关文章

Codeforces 294B Shaass and Bookshelf(记忆化搜索)

题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[110][210][210];//dp[第几个][厚度][宽度] int n; int a[110],b[110]; int rec(int i,int th,

codeforces 294B Shaass and Bookshelf

题意:给你n本书,你可以把它竖着摆放,然后也可以横着摆在竖着的书上面,但不能超过竖着摆放的边缘,且不可以堆叠.问你竖着摆放的最小宽度是多少. 解题思路:dp,dp[i][j] 代表 第i个 ,用竖着摆放为 j 的书横着摆放的最小值 解题代码: 1 /************************************************************ 2 * Author : darkdream 3 * Email : [email protected] 4 * Last mo

Codeforces 294B Shaass and Bookshelf:dp

题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, 1<=w[i]<=100). 然后让你将所有书按照下面的方式摆放: 在下面放一本书会占用下面t[i]的长度. 在上面放一本书会占用上面w[i]的长度. 最终要保证上面的总长度不超过下面的总长度. 问你下面的总长度最小是多少. 题解: 表示状态: dp[i][j] = min length 表示已经

codeforces A. Shaass and Oskols 题解

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

Codeforces 97D Robot in Basement bitset+模拟

题目链接:点击打开链接 题意: 每个点有一个机器人(.),下面是一些指令,每次发出指令(一个字母)所有机器人都会执行移动. 当机器人到E点就会离开. 若机器人前方是'#' 或者边界则停留原地.一个方格可以站多个机器人. bitset模拟.. #include <cstdio> #include <cstring> #include <algorithm> #include <bitset> using namespace std; char s[100005

Codeforces 12D Ball 树状数组模拟3个元素的排序

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

codeforces 459C Pashmak and Buses(模拟,组合数A)

题目 跑个案例看看结果就知道了:8 2 3 题目给的数据是 n,k,d 相当于高中数学题:k个人中选择d个人排成一列,有多少种不同的方案数,列出其中n中就可以了. #include<iostream> #include<algorithm> #include<string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

Codeforces 309C Memory for Arrays 二进制模拟进位

题目链接:点击打开链接 题意: 给定n个箱子m个物品 下面n个数字表示箱子的容量 下面m个数字b1-bm 表示物品体积为2^bi大 问最多有多少个物品可以放入箱子. 思路: 贪心,先放小的,小的不能放再放大的 显然我们把n个箱子拆成二进制,然后模拟二进制减法运算. 剩下就是简单模拟 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<m

CodeForces 28D Don&#39;t fear, DravDe is kind dp

题目链接:点击打开链接 要使得删除后车队是合法的,即对于车队中的每辆车, l+r+c 都相同,则按l+r+c分类. 然后dp一下. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set> #include <vector> #include <map&g