51nod 1495:中国好区间
来源:互联网 发布:怎么学java软件工程师 编辑:程序博客网 时间:2024/06/11 21:08
1495 中国好区间
基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
收藏
关注
阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是>=k的,且该区间的第k大的那个数,一定大于等于T。那么问题来了,阿尔法想知道有多少好的区间。
由于阿尔法的序列长度实在是太大了,无法在规定时间内读入。
他想了一个绝妙的方法。
读入a[0],b,c,p,则a[i]=(a[i-1]*b+c)mod p。
样例解释:
a1~a5分别为47,135,247,35,147
对应的7个区间分别为[1,3],[2,3],[1,4],[2,4],[1,5],[2,5],[3,5]
对于重复的数字1,2,2 第一大是2,第二大也是2,第三大是1。
Input
读入一行,7个数字,表示n(n<=10000000),k(k<=n),T,a[0],b,c,p。所有数字均为正整数且小于等于10^9。
Output
输出一行表示好区间的个数。
Input示例
5 2 100 10 124 7 300
Output示例
7
官方题解:
对于一个数,我们只需要知道它与T的相对大小关系,因此题目可以转化为求存在多少区间大于等于k。
假如固定左端点,那么对于一个好区间,那么右端点一定是连续的,若左端点向右移动,那么最近右端点不可能向左移动。因此可以使用双指针来做这题。
代码:
#pragma warning(disable:4996) #include <iostream> #include <algorithm>#include <windows.h>#include <cstring>#include <cstring>#include <vector> #include <string> #include <time.h>#include <cmath>#include <queue>#include <map>using namespace std;typedef long long ll;#define INF 0x3fffffffconst int maxn = 10000005;ll n, k, T, a0, b, c, p;int che[maxn];ll val[maxn];void input(){scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &k, &T, &a0, &b, &c, &p);}void solve(){int i, kk = 0;val[1] = (a0*b + c) % p;if (val[1] >= T){che[1] = 1;}for (i = 2; i <= n; i++){val[i] = (val[i - 1] * b + c) % p;if (val[i] >= T){che[i] = 1;}}ll res = 0;int lef = 1, rig = 1;while (rig <= n){if (che[rig]){kk++;}if (kk == k){break;}rig++;}while (rig <= n&&rig - lef + 1 >= k){res += (n - rig + 1);if (che[lef]){while (rig <= n){rig++;if (che[rig]){break;}}}lef++;}cout << res;}int main(){//freopen("i.txt", "r", stdin);//freopen("o.txt", "w", stdout);input();solve();//system("pause");return 0;}
0 0
- 51nod 1495:中国好区间
- 51nod 1495 中国好区间
- 51nod 1495 中国好区间
- 51nod 1495 中国好区间
- 51Nod-1495-中国好区间
- 51nod 1495 中国好区间【2-pointers】
- 51nod 1495中国好区间 && qduoj78 阿尔法狗的难题 (尺取法)
- 51nod 1495 中国好区间 详细题解(尺取+前缀和)
- 51nod1495 中国好区间
- 51 nod 1495 中国好区间 奇葩卡时间题 700ms 卡O(n-log(n)), 思路:O(n)尺取法
- 51 nod 1495 中国好区间 奇葩卡时间题 700ms 卡O(n-log(n)), 思路:O(n)尺取法
- 51 nod 1495 中国好区间 奇葩卡时间题 700ms 卡O(n*log(n)), 思路:O(n)尺取法
- 51nod1495 中国好区间 思维
- 51NOD1495 中国好区间 【尺取法】
- 【bzoj1495】中国好区间
- 51nod 1686 第K大区间 二分好题
- 好数 51Nod
- 51nod 1434 区间LCM
- 图像处理和图像识别中常用的OpenCV函数
- 安卓版本微信多开
- /** * Describe: * User: fanweixiao * Date: ${date} * Time: ${time} */
- iOS中多个storyboard之间的跳转
- Jquery Table 的基本操作
- 51nod 1495:中国好区间
- 值得收藏的开源代码之UI篇
- gitub优秀的android开源项目
- solr Query
- js获取当前页面的url网址信息
- ARM(五) Thumb 指令集
- 位运算符笔记
- hadoop2.6.0 在64位CentOS 6.4系统上的编译
- TI DSP TMS320C66x学习笔记之TI Imglib库相关函数(四)