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 ...
随机推荐
- SPF难以解决邮件伪造的现状以及方案
邮件伪造的现状 仿冒域名 私搭邮服仿冒域名: 例如某公司企业的域名是example.com,那么攻击者可以搭建一个邮服,也把自己的域名配置为example.com,然后发邮件给真实的企业员工xxx@e ...
- Intellij IDEA同时打开多个项目
extends:http://www.kaifazhe.me/java/99.html 使用eclipse习惯的同学知道是可以同时多个项目查看的,只需要import就可以了,但Intellij IDE ...
- java定时器学习
一.这个是利用jdk自带的Thread类的sleep方法实现定时执行任务. package tasker; import java.util.Date; public class tasker01 e ...
- 网络费用流-最小k路径覆盖
多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- C#生成流水号编码[a-z(不包括i和o) 按0-9 a-z的顺序)]
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; nam ...
- LightOj 1422 Halloween Costumes(区间DP)
B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- Django之views.py详解
http请求中产生的两个核心对象: http请求:HttpRequesthttp响应:HttpResponse 所在位置:from django.http import HttpRequest,Htt ...
- 洛谷P1736 创意吃鱼法 dp
正解:dp 解题报告: 早就想写dp的题目辣!我发现我的dp好差啊QAQ所以看到列表的小朋友写dp的题目就跟着他们的步伐做下题好辣QwQ 这题的话没有那——么难,大概说下趴QwQ 首先说下题意 前面一 ...
- ASP.Net Core 使用Redis实现分布式缓存
本篇我们记录的内容是怎么在Core中使用Redis 和 SQL Server 实现分布式缓存. 一.文章概念描述 分布式缓存描述: 分布式缓存重点是在分布式上,相信大家接触过的分布式有很多中,像分 ...
- Cardano(ADA), EOS, RChain(RHOC), Aeternity(AE) 都是极其好的币
从区块链的基础知识出发,研究ETH和EOS的区别 免责声明:EOS目前还在开发中,我们对此项目的一些理解可能会改变.而且,我并不是以太坊开发者,而只是一个喜欢区块链的爱好者.请牢记这两点,请把下面的内 ...