这题说的是给了一个数在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的更多相关文章

随机推荐

  1. 【转】失效迭代器(Invalidating Iterators)

      当一个容器变化时,指向该容器中元素的迭代器可能失效.这使得在迭代器变化期间改变容器容易出现问题.在这方面,不同的容器提供不同的保障:vectors: 引起内存重新分配的插入运算使所有迭代器失效,插 ...

  2. Node学习HTTP模块(HTTP 服务器与客户端)

    Node学习HTTP模块(HTTP 服务器与客户端) Node.js 标准库提供了 http 模块,其中封装了一个高效的 HTTP 服务器和一个简易的HTTP 客户端.http.Server 是一个基 ...

  3. like to do vs like doing

    I like to eat apple 表示我喜欢吃苹果这种食物. I like eating apple 表示我喜欢吃苹果这种食物 或者 表示我喜欢吃苹果这个过程. like to do,表达的是倾 ...

  4. POJ-1887 Testing the CATCHER(dp,最长下降子序列)

    Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16515 Accepted: 6082 ...

  5. XTU 1264 - Partial Sum - [2017湘潭邀请赛E题(江苏省赛)]

    2017江苏省赛的E题,当时在场上看错了题目没做出来,现在补一下…… 题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id ...

  6. C++中引用与取地址

    所谓引用就是为对象起一个别名.例如变量b = &a,b就是a的一个引用.对b的任何操作等同于对a的操作,也就是说,如果你改变了b的值,同时a的值也会发生改变.b就是a的另外一个名字,他们实质是 ...

  7. Oracle字符串连接的方法

    Oracle数据库中,使用“||”进行字符串连接,下面就让我们一起了解一下Oracle数据库中字符串连接的方法,希望对您能有所帮助. 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串 ...

  8. POJ - 1054 The Troublesome Frog 模拟 枚举优化。

    题意:有个R*C的格网.上面有若干个点,这些点可以连成一些直线,满足:这些点在直线上均匀排布(也就是间隔相等),直线的两段穿过网格(也就是第一个,最后一个在网格的边界附近) 求某条直线上最多的点数 题 ...

  9. MySQL InnoDB的存储结构总结

    从物理意义上来讲,InnoDB表由共享表空间.日志文件组(redo文件组).表结构定义文件组成.若将innodb_file_per_table设置为on,则系统将为每一个表单独的生成一个table_n ...

  10. 自己封装framworks上传到应用商店报错

    参考链接: http://www.jianshu.com/p/60ac3ded34a0 http://ikennd.ac/blog/2015/02/stripping-unwanted-archite ...