hdu 6047 Maximum Sequence【思维】
来源:互联网 发布:淘宝奇葩商品 二向箔 编辑:程序博客网 时间:2024/06/03 16:08
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 74 Accepted Submission(s): 34
Problem Description
Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one day Klay comes up with a problem and ask him about it.
Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}:an+1…a2n . Just like always, there are some restrictions on an+1…a2n : for each number ai , you must choose a number bk from {bi}, and it must satisfy ai ≤max{aj -j│bk ≤j<i}, and any bk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai } modulo 109 +7 .
Now Steph finds it too hard to solve the problem, please help him.
Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}:
Now Steph finds it too hard to solve the problem, please help him.
Input
The input contains no more than 20 test cases.
For each test case, the first line consists of one integer n. The next line consists of n integers representing {ai}. And the third line consists of n integers representing {bi}.
1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.
For each test case, the first line consists of one integer n. The next line consists of n integers representing {ai}. And the third line consists of n integers representing {bi}.
1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.
Output
For each test case, print the answer on one line: max{∑2nn+1ai } modulo 109 +7。
Sample Input
48 11 8 53 1 4 2
Sample Output
27HintFor the first sample:1. Choose 2 from {bi}, then a_2…a_4 are available for a_5, and you can let a_5=a_2-2=9; 2. Choose 1 from {bi}, then a_1…a_5 are available for a_6, and you can let a_6=a_2-2=9;
Source
2017 Multi-University Training Contest - Team 2
题意:
给你一个数n,然后给你两个序列长度为n的a序列和b序列,它们的取值范围(不是取值而是范围,b数组可以都是1只要小于n就好)1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.然后让你找到a序列的An+1到An+n,并求出An+1到An+n的求和最大值最后取模,对每个ai找最优解的条件限制
思路:
注意第n+1位是n+1~2n之中最大的那个数(没必要去动态维护这个序列),结构体排序也可以(记录下标就可以了)或者倒过来打表更新前n位最大值,然后就是处理第n+1位了,让目前的maxx(下标最小才能保证maxx最大,下标最小也就是b[1])减去下标n+1构成第n+1位。注意maxx[i]数组存储的是i~n位的最大值,最后要比较第n+1位;
#include<cstdio>#include<algorithm>#include<cstring> #include<cmath>#define max_n 250010typedef long long LL;const int mod=1e9+7;using namespace std;LL b[max_n],maxx[max_n],a[max_n];int main(){int n;while(scanf("%d",&n)!=EOF){memset(maxx,0,sizeof(maxx));LL maxn=0,sum=0,p;for(int i=1;i<=n;i++){scanf("%lld",&a[i]);a[i]-=i; //减去下标 }for(int i=1;i<=n;i++)scanf("%lld",&b[i]); //注意b范围和取值QAQ sort(b+1,b+n+1);for(int i=n;i>=1;i--)maxx[i]=max(maxx[i+1],a[i]);maxn=maxx[b[1]]-n-1;//计算第n+1位,其就是n+1~2n 最大的那个数值(且b值不大于n) for(int i=1;i<=n;i++){p=max(maxx[b[i]],maxn);sum=(sum+p)%mod;}printf("%lld\n",sum%mod);}return 0;}
greater是升序排列,后面的大于前面的
less是降序排列,后面的小于前面的
在初始化优先级队列时默认是less
priority_queue<int,vector<int>,less<int> > que与priority_queue<int > que是一样的效果
也可以自己写比较函数
struct cmp{
bool operator() ( int a , int b ){
return a< b; //与greater是等价的
}
};
priority_queue<int,vector<int>,cmp > p;
#include<cstdio>#include<algorithm>#include<cstring> #include<cmath>#include<queue>#define max_n 250010typedef long long LL;const int mod=1e9+7;using namespace std;typedef long long Ll;typedef pair<int,int> P;int a[max_n],num[max_n];int main(){int n,k;while(scanf("%d",&n)!=EOF){memset(num,0,sizeof(num));priority_queue<P,vector<P>,less<P> > que;for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]-=i;que.push(P(a[i],i));}for(int i=1;i<=n;i++){scanf("%d",&k);++num[k];}int ans=0,tol=n+1;for(int i=1;i<=n;i++){if(num[i]){while(que.top().second<i) que.pop();P p=que.top();int val=p.first,id=p.second;ans+=1LL*val*num[i];ans%=mod;for(int j=0;j<num[i];j++){que.push(P(val-tol,tol));tol++;}num[i]=0;}} printf("%d\n",ans);}return 0;}
阅读全文
0 0
- hdu 6047 Maximum Sequence【思维】
- [HDU]-6047 Maximum Sequence
- hdu 6047 Maximum Sequence
- [HDU 6047]Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu 6047 Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu 6047 Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu--6047--Maximum Sequence
- hdu 6047 Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu 6047 Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu-6047-Maximum Sequence
- HDU 6047 Maximum Sequence
- hdu 6047 Maximum Sequence
- HDU 6047 Maximum Sequence
- 栈和栈帧
- MySQL修改用户权限
- iOS开发 数据存储之YYCache
- 语义分析的一些方法(一)
- php BC高精确度函数库
- hdu 6047 Maximum Sequence【思维】
- Qt creator添加套件
- jquery实现贪吃蛇功能
- JVM中jmap无法连接java进程的问题
- http与https连接获取数据
- 计算机视觉----图像底层特征提取之边缘提取(一)
- Http长连接和管线化 Servlet NIO
- JMS-ActiveMQ系列
- 【Java并发编程】原子性访问