Gym 100712I Bahosain and Digits(开关翻转问题)
http://codeforces.com/gym/100712/attachments
题意:
给出一串数字,每次选择连续的k个数字加上任意数(超过10就取余),最后要使得所有数字都相等,求最大的k。
思路:
开关翻转问题。
算法具体可以参考《挑战程序竞赛》常用技巧篇。
这道题目就是在枚举k的同时再枚举一下最后要转换成的数字即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,long long> pll;
const int INF = 0x3f3f;
const int maxn=+; int n,k;
int a[maxn];
int b[maxn];
int f[maxn];
char str[maxn]; bool calc(int k)
{
memcpy(b,a,sizeof(a));
for(int t=;t<;t++) //枚举最后转换的数
{
memset(f,,sizeof(f));
int sum=;
for(int i=;i+k<=n;i++)
{
if(((b[i]+sum)%)!=t)
f[i]=((t-b[i]-sum)%+)%;
sum+=f[i];
if(i-k+>=) sum-=f[i-k+];
} bool flag=true;
for(int i=n-k+;i<n;i++)
{
if(((b[i]+sum)%)!=t)
{
flag=false;
break;
}
if(i-k+>=) sum-=f[i-k+];
}
if(flag) return true;
}
return false;
} int main()
{
//freopen("input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
getchar();
scanf("%s",str); n=strlen(str);
for(int i=;i<n;i++) a[i]=str[i]-''; for(int k=n;k>=;k--)
{
if(calc(k))
{
printf("%d\n",k);
break;
}
}
}
return ;
}
Gym 100712I Bahosain and Digits(开关翻转问题)的更多相关文章
- UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。
A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 浅谈Power Signoff
Power Analysis是芯片设计实现中极重要的一环,因为它直接关系到芯片的性能和可靠性.Power Analysis 需要Timing Analysis 产生包含频率.transition 等时 ...
- B. Light bulbs
B. Light bulbs There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are o ...
- Codeforces Gym 100531D Digits 暴力
Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...
- POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
POJ 1222 EXTENDED LIGHTS OUT 今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗, ...
- 翻转整数 Reverse digits of a number
两种方法翻转一个整数.顺序翻转和递归翻转 这里没考虑overflow的情况 递归的作用是使得反向处理.即从递归栈的最低端開始处理.通过绘图可得. 假设是rec(num/10): 12345 1234 ...
- Gym 100531D Digits (暴力)
题意:给定一个数字,问你找 n 个数,使得这 n 个数各位数字之和都相等,并且和最小. 析:暴力,去枚举和是 1 2 3...,然后去选择最小的. 代码如下: #pragma comment(link ...
随机推荐
- 使用servlet3.0提供的API来进行文件的上传操作
servlet 3.0针对文件上传做了一些优化,提供了一些更加人性化的API可以直接在request中的到文件的名称.文件size,MIME类型,以及用InputStream表示的文件流的信息 @Re ...
- spring低版本报错:java.lang.IllegalStateException: Context namespace element ‘annotation-config’ and its parser class [*] are only available on
参考来源:http://blog.csdn.net/sunxiaoyu94/article/details/50492083 使用spring低版本(2.5.6),使用jre 8发现错误: Unexp ...
- squid 高匿设置
1.透明代理.匿名代理.混淆代理.高匿代理有什么区别 这4种代理,主要是在代理服务器端的配置不同,导致其向目标地址发送请求时,REMOTE_ADDR, HTTP_VIA,HTTP_X_FORWARDE ...
- #cat ora11g_ora_.trc
Trace file /u02/app/diag/rdbms/ora11g/ora11g/trace/ora11g_ora_31212.trc Oracle Database 11g Enterpri ...
- 最小费用流判负环消圈算法(poj2175)
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3384 Accepted: 888 ...
- 310实验室OTL问题----将写好的C++文件转换成Python文件,并将数据可视化
如图:文件夹 第一处:optimizer文件夹下的:optimizer.h文件中添加你所写代码的头文件 #include <OTL/Optimizer/Reference-NSGA-II/Re ...
- 170804、使用Joda-Time优雅的处理日期时间
简介 在Java中处理日期和时间是很常见的需求,基础的工具类就是我们熟悉的Date和Calendar,然而这些工具类的api使用并不是很方便和强大,于是就诞生了Joda-Time这个专门处理日期时间的 ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- 比特币 BTC本地测试节点
BTC本地测试节点 #环境 ubuntu 16.4 #客户端安装 #下载页面 #https://bitcoin.org/zh_CN/download cd /opt/ wget https://bit ...
- 纯css3加载动画
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...