Codeforces#572 Div2 C---Candies!【倍增】【DP】【思维】
题目:http://codeforces.com/contest/1189/problem/C
题意:给定n个数,每次查询一个区间$[l,r]$。对这个区间内的数,相邻两个数之和超过10,则得到一个candy,然后将他们之和取模10的结果作为新的数。
一共操作$l-r$次,问这个区间得到的candy数。
思路:一种思路是可以发现,实际上candy数是和/10,而新得到的数实际上是/10之后的小数部分。
每次操作的过程其实可以看成是两两求和然后取/10的整数部分,然后把小数部分再两两求和取整数部分,剩下的再留给下一次操作。
所以$ans[l,r] = \lfloor \frac{a_l+a_{l+1}+...+a_r}{10} \rfloor$
另一种思路是用倍增的思想进行dp。
$dp[i][k]$表示以$i$为开头,$2^k$个数所得到的candy数。因为转移的时候还需要知道当前操作之后的数是什么,所以用$dig[i][k]$记录对应操作后的数。
$dp[i][k] = dp[i][k-1]+dp[i + 2^{k-1}][k-1] +(dig[i][k-1] + dig[i + 2^{k-1}][k-1] \ge 10)$
DP解法
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; const int maxn = 1e5 + ;
int n, q;
int num[maxn];
int dp[maxn][], dig[maxn][]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &num[i]);
dp[i][] = ;
dig[i][] = num[i];
}
int pow = ;
for(int k = ; k < ; k++, pow *= ){
for(int i = ; i + pow <= n + ; i++){
dp[i][k] = dp[i][k - ] + dp[i + pow / ][k - ];
if(dig[i][k - ] + dig[i + pow / ][k - ] >= )dp[i][k]++;
dig[i][k] = (dig[i][k - ] + dig[i + pow / ][k - ]) % ;
}
}
scanf("%d", &q);
while(q--){
int l, r;
scanf("%d%d", &l, &r);
int k = , seg = r - l + ;
while(seg % == ){
k++;
seg >>= ;
}
printf("%d\n", dp[l][k]);
}
return ;
}
公式解法:
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; const int maxn = 1e5 + ;
int n, q;
int num[maxn], sum[maxn]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &num[i]);
sum[i] = sum[i - ] + num[i];
}
scanf("%d", &q);
while(q--){
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", (sum[r] - sum[l - ]) / );
} return ;
}
Codeforces#572 Div2 C---Candies!【倍增】【DP】【思维】的更多相关文章
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
		https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ... 
- codeforces 985E  Pencils and Boxes(dp+思维)
		E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ... 
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
		Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ... 
- codeforces #round363 div2.C-Vacations (DP)
		题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ... 
- Codeforces 1140G Double Tree 倍增 + dp
		刚开始, 我以为两个点肯定是通过树上最短路径过去的, 无非是在两棵树之间来回切换, 这个可以用倍增 + dp 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ... 
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
		Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ... 
- Codeforces #180 div2 C Parity Game
		// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ... 
- codeforces #321 DIV2
		A题: 链接:http://codeforces.com/contest/580/problem/A dp,最长连续不上升子序列 #include<iostream> #include&l ... 
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
		Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ... 
随机推荐
- PHP,Excel导出换行
			// 有id,才算真的有发票数据 if ($v['b_invoice_id']) { $v['b_invoice_info'] = json_decode($v['b_invoice_json'],t ... 
- Request部分知识点小结
			HTTP: * 概念:Hyper Text Transfer Protocol 超文本传输协议 * 传输协议:定义了,客户端和服务器端通信时,发送数据的格式 * 特点: 1. 基于TCP/IP的高级协 ... 
- Python程序计算ax^2+bx+c=0方程根
			程序用来计算ax^2+bx+c=0的两个根,有些异常暂时无法处理: #!/usr/bin/python # -*- coding: utf-8 -*- #当程序存在中文时,注释表明使用utf-8编码解 ... 
- 又是a+b
			题目描述: 给定两个整数 a, b (a, b 均不超过 int 类型的表示范围),求出 a + b 的和.输入描述: 多组输入,每组输入为一行,里面有 2 个数 a, b.输出描述: 对于每一组输入 ... 
- 11 模块、模块的搜索顺序、__file__内置属性、__name__属性
			模块的概念 一个python文件就是一个模块. 模块名同时也是一个标识符,需要符合标识符的命名规则. 在模块中定义的全局变量.函数.类 都是个外界提供的直接使用的工具. 模块就好比工具包,要想使用一个 ... 
- 解决windows 激活问题
			解决windows 激活问题 下载 然后 搞定 重启 
- ES6语法基本使用
			什么是ES6? ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础上,推出JavaScript 2. ... 
- nfs挂载文件
			1. 安装必备插件 以防centos7默认没有启动nfs服务 yum -y install nfs-utils rpcbind # 启动 rpcbind 和配置开机自启动 systemctl sta ... 
- Consul微服务的配置中心体验篇
			Spring Cloud Consul 项目是针对Consul的服务治理实现.Consul是一个分布式高可用的系统,具有分布式.高可用.高扩展性 Consul Consul 是 HashiCorp 公 ... 
- 起始路由改成分区(Areas)的RouteConfig.cs配置方法
			public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ... 
