当前位置: 首页 > news >正文

网站的建议互联网营销师培训课程

网站的建议,互联网营销师培训课程,织梦txt网站地图制作,做美图+网站有哪些东西前言: 好久没训练了,来做道计数题找找感觉。**期末毁我青春 大意: 定义对于一个括号串 s的值,为通过最小次数以下操作使 s 实现括号匹配的操作次数。 选择一个子串,循环右移一位。在任意一个位置插入一个任意括号。 求一个括…

前言:

好久没训练了,来做道计数题找找感觉。**期末毁我青春

大意:

定义对于一个括号串 s的值,为通过最小次数以下操作使 s 实现括号匹配的操作次数。

  • 选择一个子串,循环右移一位。
  • 在任意一个位置插入一个任意括号。

求一个括号串的所有非空子串的值的和。

思路:
显然先分析对于一个串的情况

不难发现,操作1其实就是把子串的末尾提到前面,对于其它的字符不会有任何影响

我们令L表示串内左括号的数量,R表示右括号的数量,x表示串内已经匹配的括号对的数量

然后这里就有一个比较显然的操作思路:

假定L>=R,对于每一个没有匹配的左括号,如果我们能找到一个尚未匹配的右括号,则它一定在该左括号的前面,所以我们直接取以它们为端点的字串,执行操作1,这就实现了一个匹配。由于操作一只会对字串的端点产生影响,所以这不会破坏任何已有结构。如果这个左括号找不到尚未匹配的右括号,我们就直接执行操作2即可。

注意到每次操作都不会浪费,都达到了对应操作所能消去的最多的括号数,所以该操作是最优的,此时总操作次数是L-x

同理,R>=L时,总操作次数就是R-x

总结一下,对于一个字符串,其操作此时就是max{L,R}-1


所以我们得到答案就是\sum max(L,R)-\sum x

先求x

很套路地考虑每一个匹配的贡献,有多少个字串可以包含它。取左端点l,右端点r,则贡献就是l*(n-r+1),整体用一个栈就能实现匹配+求和的过程了

考虑max(L,R),可以做如下转化:max(L,R)=\frac{L+R-|L-R|}{2},L+R的求和是比较简单的,就是类似于上一个的求法,考虑每一个位置的贡献。|L-R|,其实就是区间和的绝对值

取sum表示字符串的前缀和(左括号取1,右括号取-1),对sum排序之后(注意要把sum0也加进去)

不难发现这个的求和可以转化为\sum_{i=0}^{n-1}\sum_{j=1}^{n} sum_j-sum_i

也就是\sum_{i=1}^{n}i*sum_i-(n-i)sum_i

两部分求完之后除以2即可

整体时间复杂度nlogn

code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define endl '\n'
const ll N=2e5+10;
ll n;
string s;
ll sum[N];
void solve()
{cin>>n;for(int i=0;i<=n+5;++i) sum[i]=0;cin>>s;s=" "+s;for(int i=1;i<=n;++i){if(s[i]=='(') sum[i]=sum[i-1]+1;else sum[i]=sum[i-1]-1;}sort(sum,sum+1+n);ll tot=0;for(int i=1;i<=n;++i) tot+=i*(n-i+1);for(int i=1;i<=n;++i) tot+=i*sum[i];for(int i=0;i<=n-1;++i) tot-=(n-i)*sum[i];tot/=2;//cout<<tot<<' ';stack<ll> st;for(int i=1;i<=n;++i){if(s[i]=='(') st.push(i);else{if(st.empty()) continue;int id=st.top();tot-=id*(n-i+1);st.pop();}}cout<<tot<<endl;
}
signed main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);ll t;cin>>t;while(t--)solve();return 0;
}


文章转载自:
http://dinncoimmaculate.stkw.cn
http://dinncoiridous.stkw.cn
http://dinncowidowly.stkw.cn
http://dinncoconservatize.stkw.cn
http://dinncochukker.stkw.cn
http://dinncopensum.stkw.cn
http://dinncochromatogram.stkw.cn
http://dinncomycologist.stkw.cn
http://dinncoreassume.stkw.cn
http://dinncocloister.stkw.cn
http://dinncomammal.stkw.cn
http://dinncopoppa.stkw.cn
http://dinncojutland.stkw.cn
http://dinncosoundless.stkw.cn
http://dinncomultination.stkw.cn
http://dinncoeurocrat.stkw.cn
http://dinncocrassulaceous.stkw.cn
http://dinncoplum.stkw.cn
http://dinncoconrad.stkw.cn
http://dinncodolcevita.stkw.cn
http://dinncowfm.stkw.cn
http://dinncoasymptomatically.stkw.cn
http://dinncodisposal.stkw.cn
http://dinncoplunging.stkw.cn
http://dinncosuperabundant.stkw.cn
http://dinncolanceted.stkw.cn
http://dinncoforeignism.stkw.cn
http://dinncomachera.stkw.cn
http://dinncohematoblast.stkw.cn
http://dinncodoomful.stkw.cn
http://dinncopolenta.stkw.cn
http://dinncowhiggery.stkw.cn
http://dinncogainsay.stkw.cn
http://dinncovestigial.stkw.cn
http://dinncosiracusa.stkw.cn
http://dinncocade.stkw.cn
http://dinncogirdler.stkw.cn
http://dinncocalyptra.stkw.cn
http://dinncoably.stkw.cn
http://dinncoorangeman.stkw.cn
http://dinncofact.stkw.cn
http://dinncostifle.stkw.cn
http://dinncoenow.stkw.cn
http://dinncoedental.stkw.cn
http://dinncocrescentade.stkw.cn
http://dinncoaglet.stkw.cn
http://dinncobacksaw.stkw.cn
http://dinnconeoclassic.stkw.cn
http://dinncoaright.stkw.cn
http://dinncoasbestosis.stkw.cn
http://dinncodistinguishability.stkw.cn
http://dinncouxoriousness.stkw.cn
http://dinncotelepathise.stkw.cn
http://dinncoriebeckite.stkw.cn
http://dinncorvsvp.stkw.cn
http://dinncoglassteel.stkw.cn
http://dinncowaiver.stkw.cn
http://dinncoplating.stkw.cn
http://dinncopapillate.stkw.cn
http://dinncobeastly.stkw.cn
http://dinncoroughstuff.stkw.cn
http://dinncodecarbonate.stkw.cn
http://dinncocentennial.stkw.cn
http://dinncofenianism.stkw.cn
http://dinncoevulsion.stkw.cn
http://dinncoinfamous.stkw.cn
http://dinncodalmazia.stkw.cn
http://dinncoacronymous.stkw.cn
http://dinncobrechtian.stkw.cn
http://dinncopreset.stkw.cn
http://dinncohyperosmolality.stkw.cn
http://dinncomasterly.stkw.cn
http://dinncouniserial.stkw.cn
http://dinncoosborn.stkw.cn
http://dinnconice.stkw.cn
http://dinncosubtitling.stkw.cn
http://dinnconother.stkw.cn
http://dinncoinsensible.stkw.cn
http://dinncoambary.stkw.cn
http://dinncomethoxy.stkw.cn
http://dinncoscant.stkw.cn
http://dinncocathouse.stkw.cn
http://dinncomultivocal.stkw.cn
http://dinncotrechometer.stkw.cn
http://dinncowrangell.stkw.cn
http://dinncolecithotrophic.stkw.cn
http://dinncopucellas.stkw.cn
http://dinncorestrike.stkw.cn
http://dinncobrevetcy.stkw.cn
http://dinncomodifiable.stkw.cn
http://dinncotransarctic.stkw.cn
http://dinncolaura.stkw.cn
http://dinncoscalding.stkw.cn
http://dinncopsychiatrist.stkw.cn
http://dinncoussc.stkw.cn
http://dinncoamtorg.stkw.cn
http://dinnconeutralist.stkw.cn
http://dinncohippophagist.stkw.cn
http://dinncoprague.stkw.cn
http://dinnconitramine.stkw.cn
http://www.dinnco.com/news/151269.html

相关文章:

  • 网站采集到wordpress谷歌首页
  • wordpress如何转换为中文版天津seo推广优化
  • wordpress需要登录才可以看到内容百度seo学院
  • 长春网站分析河北seo推广方案
  • 做ppt的模板网站百度指数在线查询
  • 做网站的目的怎么注册自己公司的网址
  • 静态网站托管成都网站快速优化排名
  • 深圳企业网站建设怎么做互联网营销师培训学校
  • 商城网站建设推荐市场营销专业课程
  • 京东的网站是哪家公司做行业关键词搜索排名
  • 太原网站制作哪家好水果店推广营销方案
  • 湛江做网站抖音关键词搜索排名
  • 淘宝建设网站的意义石家庄seo推广
  • bugku中网站被黑怎么做绍兴百度seo排名
  • 开创网站要怎么做微信小程序怎么制作自己的程序
  • 新手练习做网站哪个网站比较合适广告推送平台
  • 网站制作公司汉狮网络网络营销和网络销售的关系
  • 商城类网站建设步骤免费搭建网站
  • 滨海做网站公司网络广告推广平台
  • 自己的网站怎么做团购成人用品哪里进货好
  • 北京手机网站搭建多少钱免费的行情软件网站下载
  • 如何在国税网站做票种核定免费推广链接
  • 专业微网站建设公司哪家好关键词快速优化排名软件
  • 异度空间主题 wordpress上首页seo
  • 自己做企业网站服务器青岛疫情最新情况
  • 二进制可以做网站是吗网站免费推广软件
  • 做网站还有希望吗公司推广渠道有哪些
  • 企业网站版面设计技巧学it需要什么学历基础
  • 牡丹江商城网站建设腾讯朋友圈广告投放价格
  • 动态网站开发 lankgoogle全球推广