*[hackerrank]Lexicographic paths
https://www.hackerrank.com/contests/w9/challenges/lexicographic-steps
这题还是折腾很久的。题目意思相当于,比如有两个1两个0,那么找组成的数里第k大的。想法就是,如上例,假如K为4,那么先看后两位够了么C(2,2)=1,不够,那么看后三位C(3,2)=3,也不够,后四位是C(4,2)=6,够了,那么第一个1在倒数第4位。然后减去C(3,2)继续做。
#include <iostream>
#include <vector>
using namespace std; int main() {
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
k++;
string s;
s.resize(m + n);
while (m > 0) {
// find pos for the first remaining V
int x = m;
int lastR = 0;
int r = 1;
while (k > r) {
lastR = r;
x++;
r = r * x / (x - m);
}
// r >= k, fill one V
s[s.size() - x] = 'V';
m--;
k -= lastR;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] != 'V')
s[i] = 'H';
}
cout << s << endl;
}
}
*[hackerrank]Lexicographic paths的更多相关文章
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- R8:Learning paths for Data Science[continuous updating…]
Comprehensive learning path – Data Science in Python Journey from a Python noob to a Kaggler on Pyth ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- LeetCode-62-Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- web前端--知识点,笔记叠加(javascript,jquery,html5+css3.0,ajax)
函数传参列表,获取方法arguments的使用 function arg(){ var str = '总共传了'+arguments.length+'个参数\n'; for(var i=0;i< ...
- php 获取链接参数
private function getQuerystr($url,$key){ $res = ''; $a = strpos($url,'?'); if($ ...
- 防火墙设置:虚拟机ping不通主机,但是主机可以ping通虚拟机(转载)
我在Windows7系统安装了虚拟机,通过虚拟机安装了Ubuntu13.04,我设置的主机与虚拟机的连接方式是桥接,安装好后,发现虚拟机ping不通主机,但是主机可以ping通虚拟机. 我的操作是:关 ...
- 【面试虐菜】—— LVS负载均衡
Load Balancer(负载均衡器): Load Balancer是整个集群系统的前端,负责把客户请求转发到Real Server上.Load Balancer通过Ldirectord监测各Rea ...
- python之字串
python字串声明: 单引('), 双引("), 三引(''' 或 """"). python字串前缀: r表示原生字串, 字串内容: (1)不能包 ...
- C++ 字符串相关函数
<转>自:http://zhidao.baidu.com/question/173202165.html 首先就是memcpy表头文件: #include <string.h> ...
- Financial Management
Financial Management 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 Larry graduated this year and finally ...
- How to: Add SharePoint 2010 Search Web Parts to Web Part Gallery for Upgraded Site Collections
When you upgrade to Microsoft SharePoint Server 2010, some of the new SharePoint Enterprise Search W ...
- DVDRW光驱无法读DVD刻录盘
原文地址:http://www.douban.com/note/206741292/ 所有的DVD RW驱动器,不能识别DVD ROM光盘问题,都是因为DVD区域没有给定造成的,除非设备损坏!在计算机 ...
- C#开源大全--汇总
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...