Another palindrome related problem. Actually nothing too theoretical here, but please keep following hints in mind:

1. How to check whether a natural number is Palindrome
  Not sure whether there's closed form to Palindrome, I simply used a naive algorithm to check: log10() to get number of digits, and check mirrored digits.

2. Pre calculation
  1<=a<=b<=1000. so we can precalculate all Palindromes within that range beforehand.

3. Understand problem statement, only start from a Palindrome
  For each range, it must start from a Palindrome - we can simply skip non-Palindromes. And don't forget to remove all tailing non-Palindromes.

// 692 Fruit Farm
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std; /////////////////////////
#define gc getchar_unlocked
int read_int()
{
char c = gc();
while(c<'' || c>'') c = gc();
int ret = ;
while(c>='' && c<='') {
ret = * ret + c - ;
c = gc();
}
return ret;
}
int read_string(char *p)
{
int cnt = ;
char c;
while((c = gc()) == ' '); // skip spaces
//
while(c != )
{
p[cnt ++] = c;
c = gc();
}
return cnt;
}
void print_fast(const char *p, int len)
{
fwrite(p, , len, stdout);
}
/////////////////////////
bool isPalin(int n)
{
if(n >= && n < ) return true; // Get digit length
int nDigits = + (int)floor(log10(n * 1.0)); // Get separated digits
int digits[] = {};
for(int i = ; i < nDigits; i ++)
{
int d = n / (int)pow(10.0, i*1.0) % ;
digits[i] = d;
} // Check digits
bool bEven = nDigits % == ;
int inxLow = nDigits / - ;
int inxHigh = (nDigits / ) + (bEven ? : );
int nDigits2Check = nDigits / ;
for(int i = ; i < nDigits2Check; i ++)
{
if(digits[inxLow] != digits[inxHigh]) return false;
inxLow --; inxHigh ++;
}
return true;
} bool Palin[] = {false};
void precalc_palin()
{
for(int i = ; i <= ; i ++)
{
if(isPalin(i))
{
Palin[i-] = true;
//printf("%d ", i);
}
}
//printf("\n");
} void calc(int a, int b, int l)
{
int rcnt = ; int mya = , myb = ;
for(int i = a; i <= b; i++)
{
if(!Palin[i-]) continue;
//printf("At %d\n", i);
int cnt = ; int bound = min(b, i + l - );
for(int j = i; j <= bound; j ++)
{
if(Palin[j-]) cnt ++;
}
//printf("[%d, %d] = %d\t", i, bound, cnt);
if(cnt > rcnt)
{
rcnt = cnt; mya = i; myb = bound;
}
}
// shrink
if(rcnt > )
{
while(!Palin[myb-]) myb--;
printf("%d %d\n", mya, myb);
}
else
{
printf("Barren Land.\n");
}
} int main()
{
// pre-calc all palindrome in [1-1000]
precalc_palin(); int runcnt = read_int();
while(runcnt--)
{
int a = read_int();
int b = read_int();
int l = read_int();
calc(a, b, l);
} return ;
}

SPOJ #692. Fruit Farm的更多相关文章

  1. Black Beauty

    Chapter 1 My Early Home While I was young, I live upon my mother's milk, as I could not eat grass. W ...

  2. 【SPOJ】MGLAR10 - Growing Strings

    Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is us ...

  3. SharePoint 2013: A feature with ID has already been installed in this farm

    使用Visual Studio 2013创建一个可视web 部件,当右击项目选择"部署"时报错: "Error occurred in deployment step ' ...

  4. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  5. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  6. 1Z0-053 争议题目解析692

    1Z0-053 争议题目解析692 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 692.Your company wants to upgrade the production ...

  7. How To Collect ULS Log from SharePoint Farm

    We can use below command to collect SharePoint ULS log from all servers in the Farm in PowerShell. M ...

  8. How To Restart timer service on all servers in farm

    [array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...

  9. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

随机推荐

  1. 高手C++学习忠告~~[转载]

    1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...

  2. 2.0.4 CCLabelTTF在ios7下不显示的问题

    要修改框架lib目录里的 CCImage.mm,就是把浮点值取了个整,之前的浮点形导致绘制失败: 在_initWithString这个方法里把如下代码替换一下就行了: //////////////// ...

  3. Java 简介

    前言 本文大致介绍 Java 语言 什么是 Java 语言? Java 不仅仅是一门语言,Java 是一个完整的平台,有一个庞大的库,其中包含很多可重用的代码和一个提供诸如安全性,跨操作系统的可移植性 ...

  4. Java-->利用文件指针分割文件

    --> 大体上和字节流分割的方式没什么区别,只是加入文件指针确定要开始分割的位置... package com.dragon.java.splitmp3; import java.io.File ...

  5. Apache CXF 101 Win Eclipse开发环境搭建

    前言 博客草稿中“SOA生态系统初探”一文一直没有进展,感觉要将SOA.Web Service(WS).REST等概念阐述清楚还需要一些酝酿. 顶天须得立地,这里记录一些“下里巴人”的实践,主要考察A ...

  6. SAP 审计

    SAP 权限审计:SA38--- RSUSR002 还有其它事务码待续,,,

  7. 【渗透测试学习平台】 web for pentester -3.XSS

    Example 1 http://192.168.91.139/xss/example1.php?name=hacker<script>alert('xss')</script> ...

  8. Unity Shader _Time

    _Time是个4维向量,跟Unity3D中的deltaTime(这是个一维的,数值)不同. float4 _Time : Time (t/20, t, t*2, t*3), use to animat ...

  9. 【P1203】买花

    我先在已经弱到连高精乘单精都能写错的地步了QAQ 原题: 求一个小于等于N的数M,使得phi(M)/M最小,其中phi(M)是与M互质且比M小的数的个数.例如phi(4)=2,因为1,3和4互质. N ...

  10. activity状态的保存和保持(onRetainNonConfigurationInstance和getLastNonConfigurationInstanc

    本文转载于:http://chengbs.iteye.com/blog/1156167 比较onsaveinstancestate() 与 onretainnonconfigurationinstan ...