zoj3814

时间:2023-03-09 23:00:53
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 ;
}