`
Simone_chou
  • 浏览: 185184 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Juice Extractor(树状数组 + dp)

    博客分类:
  • UVA
 
阅读更多
  Juice Extractor 

Jerry loses himself in the interesting game: Fruit Ninja. Fruit Ninja is a game of iPhone and iPad in which the players cut the fruits coming from the bottom of the screen and gain the bonus from cutting more than two fruits with a single slice. Once a fruit is cut, it breaks into small pieces and cannot be cut any more.

After months of training, he becomes pro of this game. Actually, he can cut all the fruits on the screen at any time. Jerry also has a bad habit that he has no willing to leave some fruits for the future cutting. In the other words, after Jerry cuts the fruits, all the fruits on the screen breaks and no one left. That is why all his friends call him `Juice Extractor'.

Now he only consider about the bonus, when he cuts more than two fruits, he can gain some bonus scores as same as the number of fruits he slice at that time. For example, if Jerry cuts 4 fruits with a single slice, he can get 4 scores from this slice.

After Jerry gets the fruit schedule, he knows the appearing time and the disappearing time for every single fruit. He can only cut a fruit into pieces between its appearing time and disappearing time inclusive. He wants to know the maximum possible bonus scores he can receive.

 

Input 

There are several test cases; the first line of the input contains a single integer T, denoting the number of the test cases. (T$ \le$200)

For each test case, the first line contains an integer N, denoting the total number of fruits. ( 1$ \le$N$ \le$1000)

The next N lines, each line describe a fruit. For each line, there are two integers Xi and Yi, where Xi is the appearing time of the fruit and Yi is the disappearing time of this fruit. ( 0$ \le$Xi$ \le$Yi$ \le$1000000000)

 

Output 

For each test case, output a single integer denoting the maximum scores that Jerry could possibly gain. See the sample for further details.

 

Sample Input 

 

1
10
1 10
2 11
3 12
4 13
13 14
14 15
13 19
20 22
21 23
22 24

 

Sample Output 

 

 

Case #1: 10

 

     题意:

     给出 T组样例,每个样例都有一个 N,代表有 N 个水果。后给出每个水果出现的开始时间和结束时间。在任意一个时间点切水果的话,能够切到所有在这个时间出现的所有水果,当切的水果数 > 2 的时候,才会有得分,得分为所切的水果数,小于等于2得分为0。问如何切,使其分数达到最大。

 

     思路:

     DP + 树状数组。dp [ i ]  = max { dp [ t ] + point (t + 1, i) },dp [ i ] 代表在此刻切水果所达到的最大值,在 i 时刻这刀切的话,在 i 之前出现且尚未消失的水果都会被切除。

     同理 dp [ t ]  代表的也是 t 时刻前的水果都会消失,那么在 (t + 1 , i )这段时间内出现且尚未消失的水果将会在 i 这刀切下去的时候消失,所以最后的得分应该是 dp [ t ] + point ( t + 1, i ) 。

     预处理 dp 应该是只在该时刻切水果所得到的分数,用起点 + 1,(终点 + 1)-1,后 dp [ i ] += dp [ i - 1 ] 可以预处理出来。同时树状数组维护某段时间内的水果数。

 

     AC:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int MAX = 1005;

typedef struct {
    int l, r;
} node;

node no[MAX];
int ans;
int yy[MAX * 2];
int dp[MAX * 2];

int m;
int bit[MAX * 5];

int sum (int x) {
    int s = 0;
    while (x > 0) {
        s += bit[x];
        x -= x & -x;
    }

    return s;
}

void add (int i, int x) {
    while (i <= m) {
        bit[i] += x;
        i += i & -i;
    }
}

int main()
{
    int t;
    scanf("%d", &t);

    for (int tt = 1; tt <= t; ++tt) {
        int n;
        scanf("%d", &n);
        ans = 0;

        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", &no[i].l, &no[i].r);
            yy[ans++] = no[i].l;
            yy[ans++] = no[i].r;
        }

        sort(yy, yy + ans);
        ans = unique(yy, yy + ans) - yy;

        m = ans * 5;
        memset(bit, 0, sizeof(bit));
        for (int i = 1; i <= n; ++i) {
            int s = lower_bound(yy, yy + ans, no[i].l) - yy;
            add(s + 1, 1);
        }

        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n; ++i) {
            int s = lower_bound(yy, yy + ans, no[i].l) - yy;
            int e = lower_bound(yy, yy + ans, no[i].r) - yy;
            dp[s + 1] += 1;
            dp[e + 2] += -1;
        }

        for (int i = 1; i <= ans; ++i) dp[i] += dp[i - 1];

        for (int i = 1; i <= ans; ++i) {
            dp[i] = dp[i] > 2 ? dp[i] : 0;
        }

        for (int i = 1; i <= ans; ++i) {

            for (int j = 1; j < i; ++j) {
                int ans = sum(i) - sum(j);
                dp[i] = max(dp[i], dp[j] + (ans > 2 ? ans : 0));
            }

            for (int j = 1; j <= n; ++j) {
                int s =  lower_bound(yy, yy + ans, no[j].r) - yy;
                if (s + 1 == i) {
                    int e = lower_bound(yy, yy + ans, no[j].l) - yy;
                    add(e + 1, -1);
                }
            }

        }

        printf("Case #%d: ", tt);
        printf("%d\n", dp[ans]);
    }

    return 0;
}

  

 

分享到:
评论

相关推荐

    ExifLib - A Fast Exif Data Extractor for .NET 2.0

    ExifLib - A Fast Exif Data Extractor for .NET 2.0

    face-extractor:使用 face++ 从图像中提取人脸

    面部提取器 用法: 下载 安装带有 python 支持的 opencv 将文件复制到facepp-python-sdk目录中 执行pip install -r requirments.txt 使用: python extract_face.py path_to_img运行extract_face.py ...

    mediautil-1.0.jar + metadata-extractor-2.6.2.jar + xmpcore-5.1.2.jar

    java图片处理的mediautil-1.0.jar + metadata-extractor-2.6.2.jar + xmpcore-5.1.2.jar

    juice_extractor:接受测试

    gem 'juice_extractor' 然后执行: $ bundle 或将其自己安装为: $ gem install juice_extractor 用法 待办事项:在此处写下使用说明 贡献 叉它 创建功能分支( git checkout -b my-new-feature ) 提交更改...

    metadata-extractor-2.6.2-API文档-中文版.zip

    赠送jar包:metadata-extractor-2.6.2.jar; 赠送原API文档:metadata-extractor-2.6.2-javadoc.jar; 赠送源代码:metadata-extractor-2.6.2-sources.jar; 赠送Maven依赖信息文件:metadata-extractor-2.6.2.pom;...

    metadata-extractor-2.6.2-API文档-中英对照版.zip

    赠送jar包:metadata-extractor-2.6.2.jar; 赠送原API文档:metadata-extractor-2.6.2-javadoc.jar; 赠送源代码:metadata-extractor-2.6.2-sources.jar; 赠送Maven依赖信息文件:metadata-extractor-2.6.2.pom;...

    Extractor2.5

    Extractor2.5 GAL提取 资源提取 GALGAME提取器 提取器 解包工具

    mediautil+metadata-extractor

    meduautil-1.0.jar,metadata-extractor-2.3.1.jar,查看exif信息

    Maxprog Web Dumper 3.3.4 - SCH 简体破解版

    Maxprog Web Dumper 3.3.4 - SCH 简体破解版 好不容易在网上找的,在这里全都免费给大家分享了

    Universal Extractor

    Universal Extractor是一款近乎于万能的文件提取器,支持的文件类型多达40多种。无论是简单的压缩文件如zip、rar、7z,还是软件的安装程序如Inno Setup、InstallShield、Winodws Installer,抑或是一些软盘光盘镜像...

    Universal Extractor V1.6 (中文, 绿色)

    Universal Extractor 是一款近乎于万能的文件提取器,支持的文件类型多达40多种。无论是简单的压缩文件如zip、rar、7z,还是软件的安装程序如Inno Setup、InstallShield、Winodws Installer,抑或是一些软盘光盘镜像...

    PyInstaller Extractor v2.0

    PyInstaller Extractor v2.0 (Supports pyinstaller 5.3, 5.2, 5.1, 5.0.1, 5.0, 4.10, 4.9, 4.8, 4.7, 4.6, 4.5.1, 4.5, 4.4, 4.3, 4.2, 4.1, 4.0, 3.6, 3.5, 3.4, 3.3, 3.2, 3.1, 3.0, 2.1, 2.0) Author : Extreme...

    RGSSAD Extractor

    RGSSAD Extractor RGSSAD Extractor的工作原理是让游戏自己解密RGSSAD文件,当游戏把资源读入内存之后,在通过脚本抓取内存。 因此RGSSAD Extractor 存在两个缺点: 1、它提取的并不是RGSSAD包内的原始素材。你可以...

    chord-extractor:用于从多种声音文件格式中提取和弦的Python库

    和弦提取器 Python库,用于从多种格式的声音文件中提取和弦序列,并可以选择利用多重处理来快速从许多文件中获取数据。 提取过程包裹了但可扩展以轻松合并其他技术。为什么? 主要用于分析音乐作品及其和声进行的人...

    ANSYS Q3D Extractor 简明教程

    getting started with Q3D Extractor~ ansys Q3D 软件简明教程,有图形说明,和具体介绍~

    vdexExtractor 最好用的vdex odex 反编译工具

    vdexExtractor 最好用的vdex odex 反编译工具

    metadata-extractor源码及Jar包

    metadata-extractor源码及Jar包。 metadata-extractor用于获取图像的Exif信息,Exif(Exchangeable Image File)是可交换图像文件的缩写,是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据。

    DVD Audio Extractor 5.2.2 注册码

    DVD Audio Extractor 5.2.2 注册码

    metadata-extractor-2.4.0.rar 获取图片 exif 信息

    metadata-extractor-2.4.0.rar metadata-extractor-2.4.0.rar 获取 图片 exif 信息 使用方法: File jpegFile = new File("c:\\newchangetime.jpg"); Metadata metadata = JpegMetadataReader.readMetadata(jpeg...

    Easy CD-DA Extractor 12 破解文件

    Easy CD-DA Extractor (将CD或DVD音轨转成WAV RA) Easy CD-DA Extractor 将 CD 或 DVD 音轨转成 WAV 、RAW 文件的工具,你也可以配合 MPEG Layer-3 Audio Codec 直接将 CD 转成 MP3。而且它还支持从网上下载 CDDB ...

Global site tag (gtag.js) - Google Analytics