zoukankan      html  css  js  c++  java
  • CDOJ 1218 Pick The Sticks

    Pick The Sticks

    Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

    The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting idea and enjoyed it.

    Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because he's clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.

    He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.

    Formally, we can treat the container stick as an L length segment. And the gold sticks as segments too. There were many gold sticks with different length ai and value vi. Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.

    As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.

    Can you help solve the mystery by finding out what's the maximum value of the gold sticks Xiu Yang could have taken?

    Input
    The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Each test case start with two integers, N(1≤N≤1000) and L(1≤L≤2000), represents the number of gold sticks and the length of the container stick. N lines follow. Each line consist of two integers, ai(1≤ai≤2000) and vi(1≤vi≤109), represents the length and the value of the ith gold stick.

    Output
    For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum value of the gold sticks Xiu Yang could have taken.

    Sample input and output

    Sample InputSample Output
    4
    
    3 7
    4 1
    2 1
    8 1
    
    3 7
    4 2
    2 1
    8 4
    
    3 5
    4 1
    2 2
    8 9
    
    1 1
    10 3
    Case #1: 2
    Case #2: 6
    Case #3: 11
    Case #4: 3

    Hint
    In the third case, assume the container is lay on x-axis from 0 to 5. Xiu Yang could put the second gold stick center at 0 and put the third gold stick center at 5, so none of them will drop and he can get total 2+9=11 value. In the fourth case, Xiu Yang could just put the only gold stick center on any position of [0,1], and he can get the value of 3.

    Source

    The 2015 China Collegiate Programming Contest
     
    解题:又一道dp,由于重心的计算可能会不能被2整除,可以选择将长度都乘以2,不影响结果
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 1002;
     5 LL dp[2][4005][3];
     6 int a[maxn],v[maxn];
     7 void read(int &tmp) {
     8     char ch = getchar();
     9     for (; ch>'9'||ch<'0'; ch=getchar());
    10     tmp = 0;
    11     for (; '0'<=ch&&ch<='9'; ch=getchar())
    12         tmp=tmp*10+int(ch)-48;
    13 }
    14 int main() {
    15     int kase,N,L,cs = 1;
    16     scanf("%d",&kase);
    17     while(kase--) {
    18         scanf("%d%d",&N,&L);
    19         L <<= 1;
    20         LL ret = 0;
    21         for(int i = 1; i <= N; ++i) {
    22             read(a[i]);
    23             read(v[i]);
    24             a[i] <<= 1;
    25             ret = max(ret,(LL)v[i]);
    26         }
    27         memset(dp,0,sizeof dp);
    28         int cur = 0;
    29         for(int i = 1; i <= N; ++i) {
    30             for(int j = L,x = a[i]>>1; j >= 0; --j) {
    31                 for(int k = 0; k < 3; ++k) {
    32                     dp[cur^1][j][k] = dp[cur][j][k];
    33                     if(j >= x) {
    34                         if(a[i] <= j) dp[cur^1][j][k] = max(dp[cur^1][j][k],dp[cur][j-a[i]][k] + v[i]);
    35                         if(k) dp[cur^1][j][k] = max(dp[cur^1][j][k],dp[cur][j-x][k-1] + v[i]);
    36                     }
    37                     ret = max(dp[cur^1][j][k],ret);
    38                 }
    39             }
    40             cur ^= 1;
    41         }
    42         printf("Case #%d: %lld
    ",cs++,ret);
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    Sketch 画原型比 Axure 好用吗?为什么?
    ps高级磨皮的7个步骤
    算法竞赛入门经典_第二章:循环结构程序设计_上机练习_MyAnswer
    文件操作 & 重定向
    阶乘之和 & 程序运行时间 & 算法分析
    《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes
    TIJ——Chapter Two:Everything Is an Object
    LeetCode Reverse Linked List
    LeetCode Contains Duplicate
    LeetCode Contains Duplicate II
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4921775.html
Copyright © 2011-2022 走看看