![](https://i-blog.csdnimg.cn/blog_migrate/04f30444c21818e8ab316a72278ceba6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2f504b8ad7ce7b83cf4a944d6ffa8932.png)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<cstring>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#define LL long long
#define inf 0x3f3f3f3f
#define mod 1e9+7
using namespace std;
int n=0, m=0,sum=0;
int a[1005],w[1005];
int main()
{
scanf("%d %d",&n, &m);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++){
scanf("%d",&w[i]);
sum+=w[i];
}
int tmp=0;
double res=0;
for(int i=2;i<=n;i++) tmp=(tmp+m)%i;
for(int i=0;i<n;i++){
if(a[(i+tmp)%n]==1) res+=1.0*(w[i])/sum;
}
printf("%.5f\n",res);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/810ecdce3053b315ca8b4f8eade705eb.png)
![](https://i-blog.csdnimg.cn/blog_migrate/befdf9a780e5bcc535d6a9edb20d9e2a.png)
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
vector<int> findMax(vector<int>num)
{
if(num.size()==0)return num;
vector<int>res(num.size());
int i=0;
stack<int>s;
while(i<num.size()){
if(s.empty()||num[s.top()]>num[i]){
s.push(i++);
}
else{
res[s.top()]=i;
s.pop();
}
}
while(!s.empty()){
res[s.top()]=num.size();
s.pop();
}
// for(int i=0; i<res.size(); i++)
// cout<<res[i]<<endl;
return res;
}
vector<int> findMax2(vector<int>num)
{
if(num.size()==0)return num;
vector<int>res(num.size());
int i=num.size()-1;
stack<int>s;
while(i>=0)
{
if(s.empty()||num[s.top()]>=num[i])
{
s.push(i--);
}
else
{
res[s.top()]=i;
s.pop();
}
}
while(!s.empty())
{
res[s.top()]=-1;
s.pop();
}
// for(int i=0; i<res.size(); i++)
// cout<<res[i]<<endl;
return res;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// test(arr);
auto arr1 = findMax(arr);
auto arr2 = findMax2(arr);
long long ans = 0;
long long mod = 1e9+7;
for (int i = 0; i < arr.size(); i++) {
// cout << i << " " << arr1[i] - i << " " << i - arr2[i] <<endl;
// cout << i << " " << (arr1[i] - i ) * (i - arr2[i]) << " " << ((long long) arr[i]) << endl;
long long tempans = (i - arr2[i]) * ((long long) arr[i]);
tempans %= mod;
ans += (arr1[i] - i ) * tempans;
ans %= mod;
}
cout << ans << endl;
return 0;
}