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. HTTP一次请求的过程

    一次完整的HTTP请求所经历的7个步骤 HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接在HTTP工作开始之前,Web浏览器首 ...

  2. dedecms list 实现noflag

    转自:http://blog.sina.com.cn/s/blog_7e53dd2b0101l3kq.html 替换include下arc.listview.class.php即可 经测试可行 但在更 ...

  3. 六 GPU 并行优化的几种典型策略

    前言 如何对现有的程序进行并行优化,是 GPU 并行编程技术最为关注的实际问题.本文将提供几种优化的思路,为程序并行优化指明道路方向. 优化前准备 首先,要明确优化的目标 - 是要将程序提速 2 倍? ...

  4. Bandicam视频录制技巧总结+小丸工具箱压缩视频解决视频体积问题

    1.视频录制. 录制质量建议选择100,保证原文件的质量才能更好地保证渲染转码后输出视频的质量.音效这里就一个关键点,就是编码器默认的MPEG-1 L2,会导致会声会影渲染输出出错,程序强行关闭,Ve ...

  5. iOS学习笔记---oc语言第一天

    第一讲 初始类和对象 c语言的超集,允许在oc中使用c语言源代码.编译器兼容c语言程序 具备完善的面向对象特性 包含一个运行时系统 类库丰富 面向对象编程 oop 面向对象语言:c++  java   ...

  6. Codeforces Round #370 (Div. 2) A B C 水 模拟 贪心

    A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. html5的特性

    1.简化的语法更简单的doctype声明是HTML5里众多新特征之一.现在你只需要写,这就行了.HTML5的语法兼容HTML4和XHTML1,但不兼容SGML. 2. 一个替代Flash的新”canv ...

  8. 本文使用springMVC和ajax,实现将JSON对象返回到页面

    一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...

  9. leetcode 120 Triangle ----- java

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. linux查找目录下的所有文件中是否含有某个字符串

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l ...