SPOJ #692. Fruit Farm
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的更多相关文章
- 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 ...
- 【SPOJ】MGLAR10 - Growing Strings
Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is us ...
- SharePoint 2013: A feature with ID has already been installed in this farm
使用Visual Studio 2013创建一个可视web 部件,当右击项目选择"部署"时报错: "Error occurred in deployment step ' ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 1Z0-053 争议题目解析692
1Z0-053 争议题目解析692 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 692.Your company wants to upgrade the production ...
- 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 ...
- How To Restart timer service on all servers in farm
[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...
- 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 ...
随机推荐
- js缓速运动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 目标跟踪之Lukas-Kanade光流法
转载自:http://blog.csdn.net/u014568921/article/details/46638557 光流是图像亮度的运动信息描述.光流法计算最初是由Horn和Schunck于19 ...
- 使用 Spring Security 保护 Web 应用的安全
安全一直是 Web 应用开发中非常重要的一个方面.从安全的角度来说,需要考虑用户认证和授权两个方面.为 Web 应用增加安全方面的能力并非一件简单的事情,需要考虑不同的认证和授权机制.Spring S ...
- 当我把电脑500G硬盘格式化了的时候,在想些什么
所以我就想重新尝试一下怎么装多系统,bootloader,MBR到底是怎么回事儿. 简要记录一下: 首先,在可以作为启动盘的U盘里面,备份一份可以用的WinXP镜像+校园网客户端安装文件+驱动精灵安装 ...
- leetcode 143. Reorder List ----- java
Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do thi ...
- Apache模块管理
Apache是一个模块化设计的服务,核心只包含主要功能,扩展功能通过模块实现,不同模块可以被静态的编辑进程序,也可以动态加载. # /usr/local/apache/bin/httpd -M 查看 ...
- java多线程之:Java中的ReentrantLock和synchronized两种锁定机制的对比 (转载)
原文:http://www.ibm.com/developerworks/cn/java/j-jtp10264/index.html 多线程和并发性并不是什么新内容,但是 Java 语言设计中的创新之 ...
- 【转】免费开源的FTP软件,FileZilla
原文网址:http://baike.baidu.com/view/670329.htm?fr=aladdin FileZilla FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版 ...
- Java之JUC系列:外部Tools
前面写了两篇JDBC源码的文章,自己都觉得有点枯燥,先插一段JUC系列的文章来换换胃口,前面有文章大概介绍过JUC包含的东西,JUC体系包含的内容也是非常的多,不是一两句可以说清楚的,我这首先列出将会 ...
- Android Studio导入Project的方法
Android Studio到现在已经发展到0.8+的版本了,最近也在试着使用它,原因是多方面的,一个毕竟是未来的趋势,二则是github上越来越多的大牛开源项目都是基于Android Studio的 ...