zoj3814
这题说的是给了一个数在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 ;
}
zoj3814的更多相关文章
随机推荐
- [转]Shell脚本之无限循环的两种方法
方法一: while循环,用的比较多的 #!/bin/bash set j= while true do let "j=j+1" echo "----------j is ...
- linux 系统信息查看
查看系统版本:lsb_release -a 查看内核版本:uname -a 查看cpu型号:cat /proc/cpuinfo 查看硬盘空间情况df -lm 查看内存:free -m VGA显卡:l ...
- Zookeeper可以干什么
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- php时间戳和日期转换,以及时间戳和星期转换
$this->created_at为时间戳值,转换日期如下 date('m.d',$this->created_at) : y 代表年的后两位如 17 ,Y 代表 2017 , m 代 ...
- php无限极分类递归与普通
1. 递归 public function getInfo(){$data=$this->select();$arr=$this->noLimit($data,$f_id=0,$level ...
- kernel中文件的读写操作可以使用vfs_read()和vfs_write
需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...
- Docker镜像制作
使用docker原始源为centos制作一个nginx镜像 pull一个centos镜像 docker pull centos 运行进入容器 docker run -it centos 容器内安装wg ...
- POJ-1191-棋盘分割(动态规划)
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13593 Accepted: 4846 Description 将一个 ...
- oracle union 用法
[sql] view plaincopyprint?众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括以下字段与数据: drop t ...
- laravel composer install 报错解决方法
composer install 报错信息: 报错原因参考:http://blog.csdn.net/yicixing7/article/details/55050140 解决方法: 把compose ...