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 ...
随机推荐
- Spring AOP配置文件
在<aop:config>...</aop:config>报错: Multiple annotations found at this line: - cvc-complex- ...
- Java程序如何生成Jar、exe及安装文件
http://blog.csdn.net/luoweifu/article/details/7628006/ 一.用Eclipse生产Jar文件 首先,看一下我的项目的目录结构: 1,项目名字上面点右 ...
- Linux之更好的使用Bash
http://www.awolau.com/linux/start-bash.html#more 接触过Linux的童鞋肯定会知道,在Linux操作系统环境下,命令行操作有时候给我们带来极大的帮助,对 ...
- Color Map的生成方法
/* Return a RGB colour value given a scalar v in the range [vmin,vmax] In this case each colour comp ...
- 配置DNS
1.将DNS解析服务器添加到:/etc/resolv.conf 2.打开DNS相关配置:sed -i 's/^hosts:[ \t]*files[ \t]*$/& dns/' /etc/nss ...
- Linux 安装rar解压工具
下载RAR安装包: http://www.rarsoft.com/download.htm 我的是CentOS 64位: wget http://www.rarsoft.com/rar/rarlinu ...
- ARM1138@库函数速查
1. GPIO库函数 可实现的功能: 获得/设置指定管脚的方向(输入.输出)和模式(硬件控制) 获取/设置指定管脚的配置(驱动强度2/4/8/8_SCmA.管脚模式:推挽(弱上拉/弱下拉)/开漏(弱上 ...
- javascript 遍历object对象
(function(){ var str = ''; for(var i in obj){ //遍历object str += '\n'+(i+' : '+obj[i]); // i+' : '+ob ...
- CSS Position 定位属性介绍
1.介绍 1.1 说明 Position 属性:规定元素的定位类型.即元素脱离文档流的布局,在页面的任意位置显示. 1.2 主要的值 ①absolute :绝对定位:脱离文档流的布局,遗留下来的空间由 ...
- Choosing proper innodb_log_file_size
If you’re doing significant amount of writes to Innodb tables decent size of innodb_log_file_size is ...