FBI_诣起来玩1(单调队列,规律)

2023-11-21 03:59
文章标签 队列 起来 单调 规律 fbi

本文主要是介绍FBI_诣起来玩1(单调队列,规律),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

D1题Submarine in the Rybinsk Sea CodeForces - 1195D1

题意:给定f(x,y)的构造方式:

然后让我们计算

分析 我们可以考虑一下两个数的情况 123 和 234.

结果为112233 + 223344 + 122334 + 213243的和。即每个数的各个位都在间隔位上做出了两次贡献即123 中的1在100000和010000中都做出了两次贡献。

AC代码:

#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll unsigned long long
const ll mod = 998244353;
const int maxn = 1e5 + 10;int k, n;
ll dp[maxn][2], a[maxn], b[maxn];
string str[maxn];
ll ct(ll a)
{stack<ll> st;while(a){st.push(a % 10);a /= 10;}ll ans = 0;while(!st.empty()){ans = ans * 10 + st.top();ans = ans * 10 + st.top();st.pop();}return ans % mod * n % mod;
}
int main()
{//cout << ct("111", "2222") << endl;std::ios::sync_with_stdio(false);while(cin >> n){ll ans = 0, d;for(int i = 0; i < n; i++){cin >> d;ans += ct(d);ans %= mod;}cout << ans << endl;}return 0;
}

E - OpenStreetMap CodeForces - 1195E (单调队列)

题意:给你一定的规则让你构造出一个m * n 的矩阵, 然后让你求得是所有a * b矩阵的最小值。

分析:这题应该是这场中最值得补的一道题,

如图所示只要i和j均大于了a,b之后每走一步都会有一个最小值。然后你可以发现这题可以用单调队列来做,即先维护1 * b的矩阵,单调队列区间维护长为b区间内的最小值。然后再相同的维护在1*b基础上1*a的最小值,这样一来我们就可以得到a * b的最小值。

单调队列模板

for(int i = 1; i <= n; i++)
{while(!pre.empty()) pre.pop_back();for(int j = 1; j <= m; j++){while(!pre.empty() && pre.front() <= j - b) pre.pop_front(); // 超出区间的不再计算从前部出队列while(!pre.empty() && h[i][pre.back()] >= h[i][j]) pre.pop_back(); // 维护的是单调减pre.push_back(j);minn[i][j] = h[i][pre.front()];}
}

AC代码:

#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
const int maxn = 3010;int n, m, a, b;
ll g[maxn * maxn], x, y, z;
ll h[maxn][maxn], minn[maxn][maxn];
deque<ll> pre;
int main()
{while(~scanf("%d%d%d%d", &n, &m, &a, &b)){scanf("%lld%lld%lld%lld", &g[0], &x, &y, &z);for(int i = 1; i < maxn * maxn; i++)g[i] = (g[i - 1] *x + y) % z;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++)h[i][j] = g[(i - 1) * m + j - 1];}ll ans = 0;while(!pre.empty()) pre.pop_back();for(int i = 1; i <= n; i++){while(!pre.empty()) pre.pop_back();for(int j = 1; j <= m; j++){while(!pre.empty() && pre.front() <= j - b) pre.pop_front();while(!pre.empty() && h[i][pre.back()] >= h[i][j]) pre.pop_back();pre.push_back(j);minn[i][j] = h[i][pre.front()];}}while(!pre.empty()) pre.pop_back();for(int j = 1; j <= m; j++){while(!pre.empty()) pre.pop_back();for(int i = 1; i <= n; i++){while(!pre.empty() && pre.front() <= i - a) pre.pop_front();while(!pre.empty() && minn[pre.back()][j] >= minn[i][j]) pre.pop_back();pre.push_back(i);if(i >= a && j >= b) ans += minn[pre.front()][j];}}printf("%lld\n", ans);}return 0;
}

G - Convert to Ones CodeForces - 997A(规律题)

题意 给定只含有0和1的字符串然后让你某一串翻转或者0 ->1 ,1->0得到全一的序列求一下最小花费。

分析:010101

两种解决方法:(1)只使用置换1->0, 0->1大约使用num次num为0的块数。

(2)先翻转成一堆再置换一次,当右边是0左边是一的时候就不断翻转,花费(num-1) * x + y

第一种花费 num * y  (2) (num - 1) * x + y

当x < y时第二种更优反之第一种更优。

#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
const int maxn = 301000;ll n, x, y;
char ch[maxn];int main()
{scanf("%lld%lld%lld", &n, &x, &y);scanf("%s", ch);int len = strlen(ch);ll sum = 0;int flag = 0, num = 0, cnt = 0;for(int i = 0; i < len; i++){if(ch[i] == '0') flag = 1;else{if(flag) num++;flag = 0;}if(i == len - 1 && flag) num++;}if(num == 0){printf("0\n");return 0;}if(x >= y) printf("%lld\n", 1ll * num * y);else printf("%lld\n", 1ll * (num - 1) * x + y);return 0;
}

 

这篇关于FBI_诣起来玩1(单调队列,规律)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/399653

相关文章

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2431 poj 3253 优先队列的运用

poj 2431: 题意: 一条路起点为0, 终点为l。 卡车初始时在0点,并且有p升油,假设油箱无限大。 给n个加油站,每个加油站距离终点 l 距离为 x[i],可以加的油量为fuel[i]。 问最少加几次油可以到达终点,若不能到达,输出-1。 解析: 《挑战程序设计竞赛》: “在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一

poj3750约瑟夫环,循环队列

Description 有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。 Input 第一行输入小孩的人数N(N<=64) 接下来每行输入一个小孩的名字(人名不超过15个字符) 最后一行输入W,S (W < N),用

POJ2010 贪心优先队列

c头牛,需要选n头(奇数);学校总共有f的资金, 每头牛分数score和学费cost,问合法招生方案中,中间分数(即排名第(n+1)/2)最高的是多少。 n头牛按照先score后cost从小到大排序; 枚举中间score的牛,  预处理左边与右边的最小花费和。 预处理直接优先队列贪心 public class Main {public static voi

POJ1631最长单调递增子序列

最长单调递增子序列 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.StringTokenizer;publ

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo