everyday two problems / 3.1
题意:
给定由小写字母组成的长度为 n 的字符串
将其所有 n * (n + 1) / 2 个子串按字典序排序
输出第 k 个子串
k > (n * (n + 1) / 2) 就输出"No such line."
1 <= n, k <= 1e5
解法:
解法很多(我不会写自动机啊
我用的解法应该算是比较简单
一位一位地去计数来确定当前位的字母
比如一开始O(n)扫描出以a开头的子串有3个
以b开头的子串有4个,而我要输出排名第5的子串
所以这一位一定是'b',后面每一位字母的确定同理
当前位是空格的时候标志着结果输出结束
代码比较简单,可以参考
但是我们要说明一下效率
一开始脑残了误以为是O(26n)
但是其实每次while中运行次数与之前已经输出的子串在原字符串中出现的次数有关
那么我们可以考虑最坏情况,10w个'a'
这时候如果要求输出排名最靠后的子串的话效率已经O(n^2)
还好我们题目有限制 k <= 1e5
所以这种做法的最坏效率其实比较玄学(但是过了就好了233
#include <cstdio>
#include <vector>
#include <cstring> using namespace std; typedef long long ll; vector <int> p[], pp; char str[]; ll cnt[]; int n, m; int main() {
scanf("%s %d", str, &m);
n = strlen(str);
if(1ll * n * (n + ) / < m) {
puts("No such line.");
return ;
}
for(int j, i = ;i < n;i ++) {
j = str[i] - ;
cnt[j] += n - i;
p[j].push_back(i);
}
while() {
int pos;
for(int i = ;i <= ;i ++) {
if(cnt[i] >= m) {
pos = i;
if(!pos) return ;
putchar( + i);
break;
}
else m -= cnt[i];
}
for(int i = ;i <= ;i ++) {
cnt[i] = ;
if(i != pos) p[i].clear();
}
cnt[] = p[pos].size();
for(int k, j, i = ;i < p[pos].size();i ++) {
j = p[pos][i] + ;
if(j >= n) continue;
k = str[j] - ;
cnt[k] += n - j;
if(k == pos) pp.push_back(j);
else p[k].push_back(j);
}
p[pos].clear();
for(int i = ;i < pp.size();i ++)
p[pos].push_back(pp[i]);
pp.clear();
}
}
题意:
给定数组a[2000],-1e9 <= a[i] <= 1e9
最多允许修改 k(k <= n) 个元素
求 max(abs(a[i] - a[i + 1])) 的最小值
解法:
这题是看别人题解做的
猜测正解效率可能n^2
但并没有什么策略能直接由条件得到答案
满足单调,考虑二分,可能效率n^2logn,可以接受
然后这个judge函数就比较神了
考虑dp,f[i]表示先只考虑前 i 个且第 i 个不动的情况下
最少修改多少个能够让前 i 个的 max(abs(a[i] - a[i + 1])) <= mid
转移方程 f[i] = min(f[i], f[j] + i - j - 1)
转移条件是abs(f[i] - f[j]) <= (i - j) * mid
即,保证i j 都不变,(i, j)都改变
要能够使[i, j]任意相邻两个之差 <= mid才能转移
上面我们提到f[i]的意义,只考虑了前 i 个
但我们考虑最优解一定有最后连续 t 个都是要被修改的(0 <= t < n)
所以我们计算完所有 f[i] 之后
在从 1 到 n 扫一遍 f[i] + n - i
是一定能够包含最优解的
当然f[i] + n - i <= k就可以返回true了
#include <cstdio> typedef long long ll; int n, k, a[], f[]; int min(int x, int y) {
return x < y ? x : y;
} int dis(int x, int y) {
if(x < y) return y - x;
return x - y;
} bool judge(ll d) {
f[] = ;
for(int i = ;i <= n;i ++) {
f[i] = ;
for(int j = i- ;j;j --)
if(dis(a[i], a[j]) <= d * (i - j))
f[i] = min(f[i], f[j] + i - j - );
if(i <= k + ) f[i] = min(f[i], i - );
}
for(int i = ;i <= n;i ++)
if(f[i] + n - i <= k)
return ;
return ;
} int main() {
scanf("%d %d", &n, &k);
for(int i = ;i <= n;i ++)
scanf("%d", &a[i]);
ll l = , r = 2e9, mid;
while(l <= r) {
mid = (l + r) >> ;
if(judge(mid)) r = mid - ;
else l = mid + ;
}
printf("%I64d", l);
return ;
}
everyday two problems / 3.1的更多相关文章
- everyday two problems / 3.11 - 3.17
7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches
About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...
- 228. Summary Ranges (everyday promlems) broken problems
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- List of NP-complete problems
This is a list of some of the more commonly known problems that are NP-complete when expressed as de ...
- Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译
本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...
- Problems about trees
Problems (1) 给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)再回到起点的最短路程. 答案是显然的:边权之和的两倍. (2)给一棵带边权的树,求遍历这棵树(每个节点至少经过一次)的最 ...
- Problems with MMM for mysql(译文)
Problems with mmm for mysql posted in MySQL by shlomi 原文:http://code.openark.org/blog/mysql/problems ...
随机推荐
- raid5什么意思?如何做raid5?raid5 几块硬盘?
raid5什么意思?如何做raid5?raid5 几块硬盘? 分类: 项目管理2012-09-28 00:38 5326人阅读 评论(0) 收藏 举报 一.raid什么意思?RAID是“Redunda ...
- 【TJOI 2018】数学计算
[题目链接] 点击打开链接 [算法] 线段树维护区间乘积 [代码] #include<bits/stdc++.h> using namespace std; #define MAXQ 10 ...
- Mac 终端执行JavaScript
创建: 2017/09/16 第一步 打开命令 输入alias jsc="/System/Library/Frameworks/JavaScriptCore.framework/ ...
- 关于C++ const 变量
const 的全局变量是储存在一个只读数据段中,虽然你可以定义一个指向它的指针,却会在运行时,在对该地址赋值的时候发生运行错误,而局部的const变量是储存在栈中的,离开作用域后同样会被释放,并且可以 ...
- RAP接口文档的安装
本机环境 系统:CentOS 6.7 64 位 MySQL 5.6 JDK 1.8 Tomcat 8 Redis 3.0.7 Rap 0.14.1 Rap 说明 官网:https://github.c ...
- Win7 + VS2015 + Python3.6编译
0. 下载安装hg. http://bitbucket.org/tortoisehg/files/downloads/tortoisehg-4.0.1-x64.msi 1. 下载Python3.6源代 ...
- Elasticsearch之CURL命令的DELETE
也可以看我写的下面的博客 Elasticsearch之curl删除 Elasticsearch之curl删除索引库 删除,某一条数据,如下 [hadoop@master elasticsearch-] ...
- Linq学习(零)-错误汇总
问题一: Cannot execute text selection: CS0009 Metadata file 'C:\Users\Kimisme\Documents\LINQPad Plugins ...
- SVN系列学习(一)-SVN的安装与配置
1.SVN的介绍 SVN是Subversion的简称,是一个开发源代码的版本控制系统,采用了分支管理系统. 文件保存在中央版本库,除了能记住文件和目录的每次修改以外,版本库非常像普通的文件服务器.你可 ...
- win32绘图基础
获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...