HDU 5875 Function (单调栈+暴力)
来源:互联网 发布:小米最美女程序员离职 编辑:程序博客网 时间:2024/06/05 10:00
Function
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2669 Accepted Submission(s): 922
Problem Description
The shorter, the simpler. With this problem, you should be convinced of this truth.
You are given an arrayA of N postive integers, and M queries in the form (l,r) . A function F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculateF(l,r) , for each query (l,r) .
You are given an array
You job is to calculate
Input
There are multiple test cases.
The first line of input contains a integerT , indicating number of test cases, and T test cases follow.
For each test case, the first line contains an integerN(1≤N≤100000) .
The second line containsN space-separated positive integers: A1,…,AN (0≤Ai≤109) .
The third line contains an integerM denoting the number of queries.
The followingM lines each contain two integers l,r (1≤l≤r≤N) , representing a query.
The first line of input contains a integer
For each test case, the first line contains an integer
The second line contains
The third line contains an integer
The following
Output
For each query(l,r) , output F(l,r) on one line.
Sample Input
132 3 311 3
Sample Output
2
Source
2016 ACM/ICPC Asia Regional Dalian Online
POINT:
用next数组保存比这个数小的数的位置,在循环找下去,找到最小的就是。
法1 单调栈 法2 两个for循环暴力。
法1:
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>using namespace std;int a[100003];int nxt[100003];int main(){ int n; int T; scanf("%d",&T); while(T--) { stack<int> q; while(!q.empty()) q.pop(); scanf("%d",&n); for(int i=1;i<=n;i++) { nxt[i]=-1; scanf("%d",&a[i]); while(!q.empty()&&a[q.top()]>=a[i]) { nxt[q.top()]=i; q.pop(); } q.push(i); } int m; scanf("%d",&m); while(m--) { int l,r; scanf("%d %d",&l,&r); int now=a[l]; int k=nxt[l]; while(k!=-1&&k<=r) { now=now%a[k]; k=nxt[k]; } printf("%d\n",now); } } }
法2: 就这个片段不一样。
for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(a[i]>=a[j]) { nxt[i]=j; break; } } }
阅读全文
0 0
- hdu 5875 Function 单调栈 + 暴力
- HDU 5875 Function (单调栈+暴力)
- [HDU 5875] Function (单调栈/(RMQ+二分))
- HDU 5875 Function(单调栈+在线倍增法)
- HDU-5662 YJQQQAQ and the function (枚举&&单调栈)
- HDU 6205 card【单调队列思想+暴力】
- hdu 5875 单调栈+离线
- hdu 3855 单调栈
- hdu 4923 单调栈
- HDU 4252(单调栈)
- HDU 1506 单调栈
- hdu 4252(单调栈)
- HDU 5033 单调栈
- hdu 3410 单调栈
- HDU 3410【单调栈】
- HDU 1506【单调栈】
- hdu 5033(单调栈)
- hdu5662 YJQQQAQ and the function (单调栈)
- PAT+乙1008. 数组元素循环右移问题
- <统计学习方法>1 概论
- CentOS下安装JDK的三种方法
- 《机器学习》阅读心得---三、线性模型
- ftp传文件遇到不能传中文的解决方法
- HDU 5875 Function (单调栈+暴力)
- redis
- docker managed volume
- Linux-sed 命令初识
- xgboost理解
- scp和sftp不能用的解决方法
- SpringIOC的使用思路(入门)
- 二.HighGUI图形用户界面初步(2.Trackbar)
- UI-控制器创建的三种方式