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 ...
随机推荐
- Mysql 千万数据快速导入
最近碰到个项目,需要 千万条数据入库的问题,有原本的 类 csv 文件导入, 统计了下 数据行大概有 1400W 行之多 二话不说, 建表,直接 load LOAD DATA LOCAL INFIL ...
- Generative Adversarial Network (GAN) - Pytorch版
import os import torch import torchvision import torch.nn as nn from torchvision import transforms f ...
- PAT(B) 1044 火星数字(Java)进制转换
题目链接:1044 火星数字 (20 point(s)) 题目描述 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, ...
- K number(思维和后缀以及3的特性)(2019牛客暑期多校训练营(第四场))
示例1: 输入:600 输出:4 说明:'600', '0', '0', '00' are multiples of 300. (Note that '0' are counted twice bec ...
- Scratch(二)来不及解释了,马上开始编程游戏
来来来,上一期你们都经过了”HelloWorld”神咒的加持,已入编程大门,我们今天就开始一边做游戏,一边熟悉Scratch. “我只是切出去抢了个红包,一回来就到了编程游戏的环节了?” 对,你没跑错 ...
- php批量检测并去除BOM头的代码
开发中会遇到BOM头, 导致程序无法执行. 浏览器返回接口如下图: 去除BOM头解决方法:<?phpini_set('memory_limit','1024M'); function check ...
- ASCII&UNICODE编码演化
ASCII 上个世纪60年代,美国制定了基于拉丁字母的一套电脑编码系统,取名为ASCII.它主要用于显示现代英语和其他西欧语言,是现今最通用的单字节编码系统. ASCII码使用指定的7位或8位二进制数 ...
- js入门之对象
一.对象理解 现实世界 万物皆对象, 一切事物都是对象 对象还是一个具体的事物 对象: 特征和行为组成 特征是名词 用来描述对象的, 行为是动词 程序中的对象 是对现实世界中事物的抽象 1. js中的 ...
- iOS滤镜功能
一.iOS自带滤镜 1.CoreImage 使用苹果自带的CoreImage框架对图片进行处理,用CoreImage框架里的CIFilter对图片进行滤镜处理, 首先我们应该了解下CoreImage框 ...
- linux命令管道符
linux多命令 ; 多个命令互相不影响 a && b a命令执行成功才执行b命令 a || b a成功不执行b a失败执行b ifconfig && echo & ...