http://community.topcoder.com/stat?c=problem_statement&pm=12706&rd=15700

这题有意思。首先要观察到,如果选定一些歌曲,最优做法就是,按照tone排序,那么这时浪费的间隔最少,是(max_tone-min_tone)。那么首先对歌曲按照tone排序,这时由于取得顺序就是从左往右,可以用DP。(比如:http://community.topcoder.com/stat?c=problem_solution&cr=23061369&rd=15700&pm=12706)但又其实对于给定的歌曲集,用贪心按照duration从小到大取就行,那么用n*n来遍历歌曲集的选取,然后用贪心选至超过T就行了。

import java.util.*;

class Song {
int duration;
int tone;
public Song(int duration, int tone) {
this.duration = duration;
this.tone = tone;
}
} public class GUMIAndSongsDiv1 {
public int maxSongs(int[] duration, int[] tone, int T) {
int len = duration.length;
Song[] songs = new Song[len];
for (int i = 0; i < len; i++) {
songs[i] = new Song(duration[i], tone[i]);
}
Arrays.sort(songs, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.tone - b.tone;
}
});
int res = 0;
for (int first = 0; first < len; first++) {
for (int last = first; last < len; last++) {
Song[] tmp = new Song[last - first + 1];
System.arraycopy(songs, first, tmp, 0, tmp.length);
int timeLeft = T - (songs[last].tone - songs[first].tone);
Arrays.sort(tmp, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.duration - b.duration;
}
});
int cnt = 0;
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].duration <= timeLeft) {
cnt++;
timeLeft -= tmp[i].duration;
}
}
res = Math.max(res, cnt);
}
}
return res;
}
}

  

*[topcoder]GUMIAndSongsDiv1的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. 如何把网站及数据库部署到Windows Azure

    http://edi.wang/Post/2014/1/1/deploying-website-with-db-to-azure-custom-domain

  2. putty 实现不用输入用户名密码直接登陆

    多谢谢Eric的教程 ,下面是我的简化版,原版为Eric所写 远程登陆Linux服务器有两大著名软件,一个是商业软件securecrt,一个是开源软件putty. 两者的安全性能都很高,发展了多年,值 ...

  3. jQuery ajax 请求php遍历json数组到table中

    html代码(test.html),js在html底部 <!DOCTYPE html> <html lang="en"> <head> < ...

  4. ASP.Net MVC中JSON处理。

    实体数据Model [Serializable] public class UserModel { //public UserModel(string name, string classname, ...

  5. Winform 异步更新listbox

    public partial class Form1 : Form { private BackgroundWorker bw; public Form1() { InitializeComponen ...

  6. AngularJS(9)-表单

    AngularJS 表单是输入控件的集合 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  7. 如何利用VS2010安装和部署应用程序

    转自:http://jingyan.baidu.com/article/4b52d70255d7f0fc5d774b4d.html 1.假设你当前的winform已经okay了 2.解决方案中新建&q ...

  8. 解决Win7系统安装时“安装程序无法定位现有 系统分区,也无法创建新的系统分区”提示

    第一步:U盘启动装系统时,格式化主分区的内容后出现上面的问题 第二步:重启机器,通过U 盘启动.进入win pe系统. 第三步:把windows 7的ISO镜像解压到电脑的非系统盘的其他硬盘上.如D: ...

  9. ubuntu 安装dell无线网卡2

    以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...

  10. Oracle RAC集群安装之:Grid软件安装过程蓝屏

    前几天在安装一套RAC服务器的过程中,遇到了蓝屏事件,折腾了蛮久(在排查是否存在硬件问题上花费了相当多一部分时间),整个过程大概说明如下: 1.两台华为的PC SERVER,操作系统为WIN SERV ...