*[topcoder]GUMIAndSongsDiv1
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的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- css笔记——关于 body/html 的高度百分比
body{ height:100%; 视窗的高度 min-height:100%;文档的具体高度} 这两个百分比的具体高度在页脚永远放在文档底部非常重要,此时用min-height:100% 具体 ...
- css笔记--web端小于1px设计的处理方法
HTML代码 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- sqlplus 可以登录 plsql 不能登录
最开始我以为是system用户被锁定了,但是解锁后仍然不可以登录.大神指导之后可以了,说是缺少监听器,解决过程如下: 1.将“tnsnames.ora”和“listener.ora”两个文件里的“lo ...
- 选择问题(选出第i个最小元素)
通过分治法解决的分析(还有其他方法解决选择问题如使用 堆) 1 同快速排序一样,对输入的数组进行递归分解 不同的是:快速排序会递归处理分解的两边,而选择问题只处理需要的一边 2 选择问题的期望时间代价 ...
- javascript之高级函数应用思想
1.级联函数:应用对象方法调用的连写 function A(){ this.a = ''; this.b = ''; this.c = ''; } //改造一下 A.prototype = { A.p ...
- PHP开发环境和软件
1/很方便的软件XAMMP集成了PHP+MYSQL+MYPHPADMIN等等软件 2/sublime text 程序员神器,都明白的 ps.如果装了vm虚拟机,80端口有时候会被占用,进程关闭就好.
- A3992学习记录
ATmega64+A3992驱动步进电机 //ATmega 64a 电机驱动板程序//编译环境 AVR Studio 4.17/AVR GCC//系统外部时钟16M//作者:虞恺 //日期:2012. ...
- RDD操作
RDD操作 1.对一个数据为{1,2,3,3}的RDD进行基本的RDD转化操作 函数名 目的 示例 结果 map() 函数应用于RDD中的每个元素 rdd.map(x=>x+1) {2,3,4, ...
- Linux ps 命令获取查询结果中的单列信息
1.查看所有进程信息,但是只想获取COMMAND列的值 SDCxM-SDCAM-root-root> ps auxUSER PID %CPU %MEM VSZ RSS TT ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...