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 ...
随机推荐
- 标准爬虫初探,来自Python之父的大餐!
首先不得不承认自己做了标题党.本文实质是分析500lines or less的crawlproject,这个project的地址是https://github.com/aosabook/500line ...
- DELL RACADM 工具使用介绍
如果iDRAC的IP或者设置出现问题,不能够链接,那么可以通过RACADM这个程序在系统层面可以对iDRAC进行设置,使用的方法以及命令都在下边. RACADM provides command li ...
- 监听tableview的点击事件
// 监听tablview的点击事件 - (void)addAGesutreRecognizerForYourView { UITapGestureRecognizer *tapGesture = [ ...
- 前端 JavaScript基础
前言 JavaScript 是属于网络的脚本语言,被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用. 一.如何编写 1.存在形式 方式一:存在js文件中,即写入j ...
- 关于Lambda表达式的理解
在.NET 1.0的时候,大家都知道我们经常用到的是委托.有了委托呢,我们就可以像传递变量一样的传递方法.在一定程序上来讲,委托是一种强类型的托管的方法指 针,曾经也一时被我们用的那叫一个广泛呀,但是 ...
- 封装Socket.BeginReceive/EndReceive支持Timeout简介
.NET中的Socket类提供了网络通信常用的方法,分别提供了同步和异步两个版本,其中异步的实现是基于APM异步模式实现,即BeginXXX/EndXXX的方式.异步方法由于其非阻塞的特性,在需考虑程 ...
- Java开发环境安装
一.安装JDK(java development kit) 下载地址:www.oracle.com/technetwork/java/javase/downloads 二.配置Java环境变量 1.J ...
- HTML5画布(变形)
坐标变换 案例1: <!DOCTYPE html><html><head lang="en"> <meta charset="U ...
- pyqt5按钮计数
万事开头难,弄了好久才做了一个简单的小程序,点击按钮就显示数字,点一下,自增1. 首先用qt设计师设计一个窗体.标签名为label,按钮名为btn,然后存储为a.ui 在shell中用命令pyuic5 ...
- C#中类的属性
1.[StructLayout] 控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递给需要指定布局的非托管代码,则显式控制类布局是重要的. 2.[Marshal ...