*[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 ...
随机推荐
- using System.Threading;
/// <summary> /// 执行动作:耗时而已 /// </summary> private void TestThread(string threadName) { ...
- 为mapcontrol中的图层设置透明度
ILayer pLayer = axMapControl1.get_Layer(); ILayerEffects pLayerEffects = (ILayerEffects)pLayer; pLay ...
- Linux一
1,debian默认需要手动开启SSH连接# Authentication:LoginGraceTime 120PermitRootLogin without-passwordStrictModes ...
- C/C++输入输出总结
*string类: 1.cin>>string时,遇到'\n'或者空格即停止,并且'\n'或空格仍留在输入里,即只读了一个单词或什么都没读,但string类自己处理好了空字符什么的.下一 ...
- IE下的bug解决方案
1.IE6下的双边距bug <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- Openvpn完美解决公司网络没有固定公网IP的问题
方案背景: 公司办公网络使用长城宽带上网有一段时间了,有4个固定IP(2个电信,2个网通),链路不太稳定,经常有问题,因此考虑取消长城宽带,采用原来的adsl上网.但是有个问题,因为公司内网有几台服务 ...
- discuz!X2.5技术文档
discuz!系统常量: DISCUZ_ROOT //网站根目录 TIMESTAMP //程序执行的时间戳 CHARSET //程序的编码类型 FORMHASH //HASH值 其余 ...
- 手工添加Linux防火墙端口
在linux实际操作中经常需要对防火墙进行修改,比如经常要修改或添加相关端口,下面以添加运行Tomcat所需8080端口为例: (以下命令操作均为root用户) 1.编辑iptables文件 # vi ...
- EditorLineEnds.ttr 受影响的D版本 Delphi 8-2010
http://stackoverflow.com/questions/25295980/delphi-2006-2010-error-cannot-create-file-c-users-admin- ...
- C#快速学习笔记(译)续一
6.虚拟和非虚拟函数 下面是一个非虚拟函数 using System; namespace Test2 { class Plane { public double TopSpeed() {return ...