HDU 6047 贪心思维题
Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1797 Accepted Submission(s): 842
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}: 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.
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
4
8 11 8 5
3 1 4 2
Sample Output
27
分析可知 ,a_n+1-------a_2*n必定是一个非严格递减序列,由此可知对a_n+1-a_2*n有贡献的只可能是a_1-a_n+1(因为a_n+1-a_2*n是一个非严格递减序列)
所以首先预处理出来从每个A[i]起的贡献Max[i],然后加一起就可以了。(注意Max数组处理时没有管道a_n+1,所以要和a_n+1进行比较一下大小)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=250008; const int mod=1e9+7; int a[N],b[N],Max[N]; int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;++i) scanf("%d",&a[i]),a[i]-=i; for(int i=1;i<=n;++i) scanf("%d",&b[i]); Max[n]=a[n]; for(int i=n-1;i>=1;--i) Max[i]=max(Max[i+1],a[i]); sort(b+1,b+n+1); int ans1=Max[b[1]]-n-1,ans=Max[b[1]]; for(int i=2;i<=n;++i) ans=(ans+max(Max[b[i]],ans1))%mod; printf("%d\n",ans); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?