2017 多校賽 第二場
阿新 • • 發佈:2017-07-28
n+1 easy and align acc gre const over color
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.
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
思路:貪心,用一個數組記下從i到n的a[i]-i的最大值pre[i],可以得到pre[i]=max(pre[i+1],a[i]-i),然後容易得到b[k]從小往大取時,後n項之和最大,所以a[n+1]=pre[b[1]](後n項會呈單調不遞增趨勢,a[n+1]-n-1即為之後取最大值的比較對象)。
代碼:
1003.Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
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 #include "cstdio" 2 #include "stdlib.h" 3 #include "iostream" 4 #include "algorithm" 5 #include "string" 6 #include "cstring" 7 #include "queue" 8 #include "cmath" 9 #include "vector" 10 #include "map" 11 #include "set" 12 #define db double 13 #define ll long long 14 #define inf 0x3f3f3f 15 using namespace std; 16 const int N=3e5+5; 17 const int mod=1e9+7; 18 #define rep(i,x,y) for(int i=x;i<=y;i++) 19 //char s[N],t[N]; 20 db pi=3.14; 21 //int s[N],w[N]; 22 int a[N],b[N]; 23 int pre[N]; 24 int main() 25 { 26 int n; 27 while(scanf("%d",&n)==1){ 28 for(int i=1;i<=n;i++){ 29 scanf("%d",a+i); 30 a[i]-=i; 31 } 32 memset(pre,0, sizeof(pre)); 33 for(int i=n;i>=1;i--) pre[i]=max(a[i],pre[i+1]); 34 for(int i=1;i<=n;i++) scanf("%d",&b[i]); 35 sort(b+1,b+n+1); 36 int ma=0; 37 ll ans=pre[b[1]]; 38 for(int i=2;i<=n;i++){ 39 ma=max(pre[b[1]]-n-1,pre[b[i]]); 40 ans=(ma+ans)%mod; 41 // printf("%d\n",ma); 42 } 43 printf("%lld\n",ans); 44 } 45 46 }
2017 多校賽 第二場