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 ...
随机推荐
- Ubuntu12.10添加matlab启动器
首先我们要了解,Ubuntu 的 Dash 里所有程序都是在 /usr/share/applications 中的,所以我们的思路很简单——建一个类似于“快捷方式”一样的东西扔进去就好了. 1. 终端 ...
- 用pytorch1.0快速搭建简单的神经网络
用pytorch1.0搭建简单的神经网络 import torch import torch.nn.functional as F # 包含激励函数 # 建立神经网络 # 先定义所有的层属性(__in ...
- Python 模块初始化的时候,发生了什么?
假设有一个 hello.py 的模块,当我们从别的模块调用 hello.py 的时候,会发生什么呢? 方便起见,我们之间在 hello.py 的目录下使用 ipython 导入了. hello.py ...
- 用 Python 把微信小程序的二维码转化成图片
官方文档 import cString import requests from tornado.web import authenticated, RequestHandler URL = 'htt ...
- java并发知识点
前言 先列出java并发涉及的知识点,后面再慢慢补. java并发 1 常见概念 进程/线程 进程:程序执行的实体,操作系统资源调度资源分配的基本单元 线程:程序执行的最小单元,拥有独立的堆栈和局部变 ...
- 发布后的项目打开swagger
使用netcore作为纯后端提供api已经变得越来越频繁,swagger也成为很多人的选择.通常会在代码中限制ASPNETCORE_ENVIRONMENT为Production时关闭swagger.但 ...
- ajax实现文件上传,多文件上传,追加参数
1.html部分实现代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- VBA分别使用MSXML的DOM属性和XPATH进行网页爬虫
本文要重点介绍的是VBA中的XmlHttp对象(MSXML2.XMLHTTP或MSXML.XMLHTTP),它可以向http服务器发送请求并使用微软XML文档对象模型Microsoft XML Doc ...
- 【转】Java最常见的200+面试题
今天看到一份面试题总结,感觉很到位,主要包括以下模块:Java基础.容器.多线程.反射.对象拷贝.Java Web模块,异常.网络.设计模式.Spring/Spring MVC .Spring Boo ...
- stm32 CAN通信 TJA1040
CAN协议特点 1.多主控制 所有单元都可以发送消息,根据标识符(Identifier简称ID)决定优先级.仲裁获胜(被判定为优先级最高)的单元可继续发送消息,仲裁失利的单元则立刻停止发送而进行接收工 ...