这题说的是给了一个数在longlong范围内 然后求出小于这个数的最大的回文,枚举每位减去1后 , 他后面的位置上全部都置为9,然后在枚举每个前半部分,然后贪心取得这个数的最大值,贪心的时候写错了,错在这..到枚举到now[loc]<now[loc+1] 时 就进行下一位,但是下一位不可能取得的时候却没有继续枚举这一位较小的

#include <cstdio>
#include <string.h>
#include <algorithm>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX_N = ;
ll str[MAX_N];
ll now[MAX_N],N,E[MAX_N],ans;
int perLen,nowLen;
void reserve(ll *C,int len){
for(int i=; i<len/; i++){
ll c = C[i];
C[i]=C[len-i-];
C[len--i]=c;
}
}
void uniquet(ll *C, int &len){
int now =;
for(int i=; i<len; i++){
if(C[i]!=C[now-]){
C[now++]=C[i];
}
}
len=now;
}
bool dfs(int loc1, int loc2, ll R)
{
if(loc2==perLen){
if(loc1==nowLen){
ans=ans>R?ans:R; return true;
}
return false;
}
if(loc1==nowLen) return false;
ll cur = R+now[loc1]*E[perLen-loc2-];
if(cur>=N) return false;
bool ans=false;
if(now[loc1]>now[loc1+]&&nowLen - loc1 < perLen - loc2){
ans= dfs(loc1,loc2+,cur);
}
if(ans==true) return true;
ans=dfs(loc1+,loc2+,cur);
if(ans==false) ans=dfs(loc1,loc2+,cur);
return ans;
}
void solve()
{
for(int i =; i<perLen; i++){
ll R =; nowLen=;
for(int j=; j<=i; ++j ){
R=R+str[j]*E[perLen--j];
now[nowLen++]=str[j];
}
uniquet(now,nowLen);
reserve(now,nowLen);
now[nowLen]=-;
dfs(,i+,R);
nowLen=;
if(i==) continue;
for(int j=; j<i; ++j ){
now[nowLen++] = str[j];
}
uniquet(now,nowLen);
reserve(now,nowLen);
now[nowLen]=-;
dfs(,i+,R);
}
}
int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int cas;
E[]=;
for(int i=; i<=; i++)
E[i]=E[i-]*10LL;
scanf("%d",&cas);
while(cas--){
scanf("%lld",&N);
if(N<){
if(N==) {
puts("");
}
else {
printf("%lld\n",N-);
}
continue;
}
int len=;
ll we = N;
while(we>){
len++; we/=;
}
ans=;
for(int i=; i< len; ++i){
ll to = N-E[i];
perLen = ;
for(int j = ; j<i; j++){
str[perLen++]=; to/=;
}
while(to){
str[perLen++]=to%;
to/=;
}
reserve(str,perLen);
solve();
}
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. [NHibernate]Nhibernate如何映射sqlserver中image字段
  2. 开发错误记录3:问题 Error:failed to find Build Tools revision 23.0.2
  3. Python Flask UnicodeDecodeError 编码错误解决
  4. zabbix 布署实践【2 agent安装】
  5. ajax---&gt;简单加法小练习
  6. Django模版基本标签详解
  7. Ubuntu 16.04 LTS 下安装 ibus-rime 输入法
  8. IIR数字滤波器的实现(C语言)
  9. LINUX UBUNTU 快捷键
  10. Java如何从文件中打印与给定模式匹配的所有字符串?
  11. FPGA按一下按键,对应端口输出单个脉冲
  12. virtualbox+vagrant学习-3-Vagrant Share-1-简介
  13. /var/log/spooler
  14. 科技巨头之微软-Microsoft
  15. CodeForces - 348A Mafia (巧妙二分)
  16. Django笔记 —— 模板高级进阶
  17. 旋转矩阵(Rotate Matrix)的性质分析
  18. C#中不用安装Oracle客户端连接Oracle数据库(转)
  19. Kettle-Data Integration 简介
  20. java之快速排序

热门文章

  1. 【BZOJ4919】[Lydsy六月月赛]大根堆 线段树合并
  2. mysql补充(2)常用sql语句
  3. Centos 密钥登录系统
  4. Java除法和js
  5. opengl学习笔记(二):使用OpenCV来创建OpenGL窗口
  6. 为什么JS事件函数里面都有一个参数(ev)?
  7. version `GLIBC_2.14&#39; not found问题
  8. python 几个重要的概念
  9. EL--Expression Language
  10. sql server dba常用概念、操作分析char,varchar,nvarchar,varchar(max)