[hdu7044]Fall with Fake Problem
二分$T$和$S$第一个不同的位置,即需要对于$s$,判定是否存在$T[1,s]=S[1,s]$且满足条件的$T$
(注:这里的条件不包含$T$的字典序最小)
显然$T[1,s]$已经确定,记其中第$i$种字母出现次数为$a_{i}$
而对于$T(s,n]$,只有字典序(尽量大)和字符数量的限制,因此若交换两个字符能增大字典序显然不劣,重复交换这样的字符,不难得到最终字符从前往后是不上升的
设其中第$i$种字符有$b_{i}$个,即希望依次最大化$b_{26},b_{25},...,b_{1}$,不妨依次暴力枚举(共$\sigma(k)$种取值),并对后面的部分背包+bitset判定是否可行,即可得到该序列并进而比较与$S$的字典序
这一部分的复杂度为$o(\alpha\sigma(k)\frac{n}{\omega}\log n)$(其中$\alpha=26$,$\omega=64$)
进一步的,由于下一个位置一定不同,那么再之后的部分也仅有字典序(尽量小)和字符数量的限制,可以类似地求出,而下一个位置直接暴力枚举即可
这一部分的复杂度为$o(\alpha^{2}\sigma(k)\frac{n}{\omega})$
综上,(单组数据)复杂度为$o(\alpha\sigma(k)\frac{n}{\omega}\log n+\alpha^{2}\sigma(k)\frac{n}{\omega})$,可以通过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 #define A 26
5 vector<int>v;
6 bitset<N>bt[A+1];
7 int t,n,m,a[A],b[A];
8 char s[N];
9 bool get_max(int n){
10 memset(b,0,sizeof(b));
11 for(int i=A-1;i>=0;i--){
12 bt[i].reset();
13 for(int j=0;(j<v.size())&&(a[i]<=v[j]);j++){
14 int x=v[j]-a[i];
15 if (x<=n)bt[i]|=(bt[i+1]<<x);
16 }
17 }
18 if (!bt[0][n])return 0;
19 for(int i=0;i<A;i++)
20 for(int j=0;(j<v.size())&&(a[i]<=v[j]);j++){
21 b[i]=v[j]-a[i];
22 if ((b[i]<=n)&&(bt[i+1][n-b[i]])){
23 n-=b[i];
24 break;
25 }
26 }
27 return 1;
28 }
29 bool check(int k){
30 memset(a,0,sizeof(a));
31 for(int i=1;i<=k;i++)a['z'-s[i]]++;
32 if (!get_max(n-k))return 0;
33 for(int i=0;i<A;i++)
34 for(int j=0;j<b[i];j++)
35 if (s[++k]!='z'-i)return s[k]<'z'-i;
36 return 1;
37 }
38 int main(){
39 bt[A][0]=1;
40 scanf("%d",&t);
41 while (t--){
42 scanf("%d%d%s",&n,&m,s+1);
43 v.clear();
44 for(int i=m;i;i--)
45 if (m%i==0)v.push_back(i);
46 v.push_back(0);
47 memset(a,0,sizeof(a));
48 if (!check(0)){
49 printf("-1\n");
50 continue;
51 }
52 int l=0,r=n;
53 while (l<r){
54 int mid=(l+r+1>>1);
55 if (check(mid))l=mid;
56 else r=mid-1;
57 }
58 if (l==n){
59 printf("%s\n",s+1);
60 continue;
61 }
62 memset(a,0,sizeof(a));
63 for(int i=1;i<=l;i++)a[s[i]-'a']++;
64 for(int i=s[l+1]-'a'+1;i<A;i++){
65 a[i]++;
66 if (get_max(n-l-1)){
67 for(int j=1;j<=l;j++)printf("%c",s[j]);
68 printf("%c",i+'a');
69 for(int j=0;j<A;j++)
70 while (b[j]--)printf("%c",j+'a');
71 printf("\n");
72 break;
73 }
74 a[i]--;
75 }
76 }
77 return 0;
78 }
[hdu7044]Fall with Fake Problem的更多相关文章
- IOS UITableView分组与索引分区实例
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- Problem of saving images in WPF (RenderTargetBitmap)zz
To save a visual to an image file need to use RenderTargetBitmap, detail is reference to Save and ...
- fake gucci outlet perform a couple associated with things in great trust
Based on my a lot of years of encounter within Taobao, purchase bags must go to the high reputation ...
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
- Bag Problem
Bag Problem Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/131072 K (Java/Others) Total ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- C#学习日志 day10 -------------- problem statement
Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem D. Grumpy Cat 交互题
Problem D. Grumpy Cat 题目连接: http://www.codeforces.com/gym/100253 Description This problem is a littl ...
随机推荐
- keepalived 安装和配置解析
Keepalived的特性 配置文件简单:配置文件比较简单,可通过简单配置实现高可用功能 稳定性强:keepalived是一个类似于layer3, 4 & 7交换机制的软件,具 ...
- NOIP模拟73
T1 小L的疑惑 解题思路 第一眼不是正解,又是 bitset 优化可以得到的 60pts 的部分分. 打着打着突然发现这个东西好像和之前做过的某个题有一些相似,试着打了一下. 然后样例过了,然后对拍 ...
- 从零入门 Serverless | 函数计算的开发与配置
导读:在本篇文章中,"基本概念"部分主要对函数计算最核心的概念进行详细介绍,包括服务.函数.触发器.版本.别名以及相关的配置:"开发流程"部分介绍了基于函数计算 ...
- Analysis Tools(分析工具)
分析工具 1.叠加分析 # Process: 交集取反 arcpy.SymDiff_analysis("", "", 输出要素类, "ALL" ...
- 题解 CF468C Hack it!
题目传送门 Description 设 \(f(i)\) 表示 \(i\) 的数码只和,给出 \(a\),求出 \(l,r\) 使得 \(\sum_{i=l}^{r} f(i)\equiv 0\pmo ...
- 基本的bash shell命令
目录 基本的bash shell命令 启动shell shell提示符 基本的bash shell命令 启动shell GNU bash shell 能提供对Linux系统的交互式访问.它是作为普通程 ...
- 使用ShardingSphere-JDBC完成Mysql的分库分表和读写分离
1. 概述 老话说的好:选择比努力更重要,如果选错了道路,就很难成功. 言归正传,之前我们聊了使用 MyCat 实现Mysql的分库分表和读写分离,MyCat是服务端的代理,使用MyCat的好处显而易 ...
- selenium3 利用cookie实现免登陆
1.首先访问要操作的页面 2.登陆一次,使用Fiddle等工具抓取出cookie 3.按照如下代码,即可成功登陆 from selenium import webdriver url = " ...
- FastAPI 学习之路(四十二)定制返回Response
我们想要在接口中返回xml格式的内容,我们应该如何实现呢. from fastapi import FastAPI,Response @app.get("/legacy/") de ...
- ThreadLocal部分源码分析
结构演进 早起JDK版本中,ThreadLocal内部结构是一个Map,线程为key,线程在"线程本地变量"中绑定的值为Value.每一个ThreadLocal实例拥有一个Map实 ...