标签:scan main line tor com comm else 最大 type
题意:每行给出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\n", ans - 100000); } return 0; }
POJ 2184 Cow Exhibition (01背包)
标签:scan main line tor com comm else 最大 type
原文地址:http://www.cnblogs.com/dwtfukgv/p/6507152.html