• POJ 2184 Cow Exhibition (01背包)


    题意:每行给出si和fi,代表牛的两个属性,然后要求选出几头牛,是的则求出总S与总F的和,注意S与F都不能为负数

    析:用dp[i]来表示存放s[i]的时最大的f[i],其实就是一个01背包。只是取不取的关系。注意是有负数,所以把数组开大一点,然后s[i]的正负数,

    我们取的顺序不同,正数是逆向,负数是正向,要不然可能有重复。还不知道为什么交G++就RE,交C++就能过。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<double, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int mod = 1e6;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    int dp[maxn*2];
    int s[105], t[105];
    
    int main(){
      while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++i)  scanf("%d %d", s+i, t+i);
        memset(dp, -INF, sizeof dp);
        dp[100000] = 0;
        for(int i = 0; i < n; ++i){
          if(s[i] <= 0 && t[i] <= 0)  continue;
          if(s[i] > 0){
            for(int j = 200000; j >= s[i]; --j)
              if(dp[j-s[i]] > -INF)  dp[j] = max(dp[j], dp[j-s[i]]+t[i]);
          }
          else {
            for(int j = s[i]; j <= 200000+s[i]; ++j)
              if(dp[j-s[i]] > -INF)  dp[j] = max(dp[j], dp[j-s[i]]+t[i]);
          }
        }
        int ans = 0;
        for(int i = 100000; i <= 200000; ++i)
          if(dp[i] >= 0)  ans = max(ans, dp[i] + i);
        printf("%d
    ", ans - 100000);
      }
      return 0;
    }
    
  • 相关阅读:
    宣布降低Windows Azure 存储和计算的价格
    物联网操作系统的概念和特点
    基于Windows8与Visual Studio11开发第一个内核驱动程序
    在 C++ 中使用 PPL 进行异步编程
    现实世界的Windows Azure:采访Figlo的全球合作伙伴支持经理Nathan Brouwer
    物联网操作系统随笔
    Windows Azure Toolkit for Windows 8 Consumer Preview的升级版发布了
    上海招聘会场所
    理解 Delphi 的类(十) 深入方法[3] 调用时参数分割
    关于类的入门例子(7): 遍历窗体的所有父类
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6507152.html
Copyright © 2020-2023  润新知