SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707
算法决定一切,这道题目有很多方法解,个人认为这里 vexorian 给出的解法最简便,编码也最容易。而使用brute force 和 DP都比较复杂。
代码如下:
#include <algorithm>
#include <iostream>
#include <sstream> #include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring> using namespace std; /*************** Program Begin **********************/ class GUMIAndSongsDiv2 {
public:
int maxSongs(vector <int> duration, vector <int> tone, int T) {
int res = 0;
int N = duration.size(); vector <int> songs;
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int maxTone = max(tone[i], tone[j]);
int minTone = min(tone[i], tone[j]);
songs.clear();
for (int k = 0; k < N; k++) {
if (tone[k] <= maxTone && tone[k] >= minTone) {
songs.push_back(duration[k]);
}
}
sort(songs.begin(), songs.end());
int sum = 0;
int c = 0;
for (int k = 0; k < songs.size(); k++) {
sum += songs[k];
if (sum <= T - maxTone + minTone) {
++c;
}
}
res = max(res, c);
}
} return res;
}
}; /************** Program End ************************/
SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了的更多相关文章
- SRM 588 D2 L3:GameInDarknessDiv2,DFS
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710 采用DFS搜索,第一次写的时候忘了加访问标志,结果状态 ...
- SRM 581 D2 L2:SurveillanceSystem,重叠度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 ...
- SRM 588 DIV1
250 题意:有n首不同的曲子,你唱每首曲子需要花费a的时间以及一个调整的时间b,调整的时间为此首歌的曲调减去上一首歌的曲调的绝对值. 思路:我们用dp[i][k]表示前i首歌只唱k首用的最小时间花费 ...
- TopCoder SRM 588 DIV2 KeyDungeonDiv2
简单的题目 class KeyDungeonDiv2 { public: int countDoors(vector <int> doorR, vector <int> doo ...
- SRM 620 DIV1 L2
题意:有n个等长的string(设string的长度为m),string中的字符从'A'到'Z',容许对m列执行稳定的排序操作,问说是否能通过这m种操作将这n个string调整成对应的顺序. 题解: ...
- SRM 585 DIV1 L2
记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向 ...
- Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法)
题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> ...
- Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)
This is the hard version of the problem. The difference is the constraint on the sum of lengths of s ...
随机推荐
- android 多项对话框
在main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- php中定界符<<<的用法
定界符给字符串定界的方法使用定界符语法(“<<<”).应该在 <<< 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串. 结束标识符必须从行的第一列开 ...
- 一键注册控件的批处理(包含x86 和 x64)
@echo off if "%PROCESSOR_ARCHITECTURE%"=="x86" goto x86 if "%PROCESSOR_ARCH ...
- NSUserDefaults读取和写入自定义对象
NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...
- 服务 进程间通讯 IPC AIDL Parcelable 简介
1.IBinder和Binder是什么鬼? 我们来看看官方文档怎么说: 中文翻译: IBinder是远程对象的基本接口,是为了高性能而设计的轻量级远程调用机制的核心部分. 但他不仅用于远程调用,也用 ...
- JAVA--聊天界面面板
package windows.beautify; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event ...
- Android推送等耗电原因剖析
原文链接:http://www.jianshu.com/p/584707554ed7 Android手机有两个处理器,一个是Application Processor(AP)基于ARM处理器,主要运行 ...
- 使用vs中的工具进行架构比较
使用vs自带的架构比较工具可以对不同库中的结构进行比较,也可以将源中的架构更新到目标架构中.当然这种更新只是架构的更新,数据并不会同步.
- ORA-25153: Temporary Tablespace is Empty解决方法
SQL> @/tmp/4.txt create table huang_1 (deptno number,dname varchar2(19),loc varchar2(20)) * ERROR ...
- C#time 闹钟
private void timer2_Tick(object sender, EventArgs e) { lbltime.Text = DateTime.Now.ToString(); & ...