第1题代码丢了,论分类整理的重要性!
按回忆补一下第1题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,ans=1e9;
map<int,set<int>> mp;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1,x;i<=n;++i){
cin>>x;
mp[x].insert(i);
}
for(auto [_,aa]:mp){
if(aa.size()<2)continue;
ll lst=-1;
for(auto it:aa){
if(lst==-1)lst=it;
else{
ans=min(ans,it-lst);
lst=it;
}
}
}
if(ans==1e9)ans=0;
cout<<ans;
return 0;
}
第2题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
char s[(int)1e5+9];
ll cnt[(int)1e5+9],sz,ed[(int)1e5+9],res;
int main()
{
scanf("%s",s+1);
sz=strlen(s+1);
for(int i=1;i<=sz;++i){
if(s[i]==s[i-1])cnt[i]=cnt[i-1]+1;
else cnt[i]=1;
}
ed[sz]=sz,ed[sz+1]=sz+1;
for(int i=sz-1;i;--i){
if(s[i]==s[i+1])ed[i]=ed[i+1];
else ed[i]=i;
}
for(int i=ed[1];i<=sz;i=ed[i+1]){
res=res+cnt[i]*cnt[i];
}
ll ans=res;
auto sol2=[](int x){
ll tmp=res-cnt[x]*cnt[x]-cnt[ed[x+1]]*cnt[ed[x+1]];
return tmp+(cnt[x]-1ll)*(cnt[x]-1ll)+(cnt[ed[x+1]]+1ll)*(cnt[ed[x+1]]+1ll);
};
auto sol3=[](int x){
ll tmp=res-cnt[x-1]*cnt[x-1]-cnt[ed[x]]*cnt[ed[x]];
return tmp+(cnt[x-1]+1ll)*(cnt[x-1]+1ll)+(cnt[ed[x]]-1ll)*(cnt[ed[x]]-1ll);
};
auto sol1=[](int x){
if(s[x-1]==s[x+1]){
ll tmp=res-cnt[x-1]*cnt[x-1]-cnt[ed[x+1]]*cnt[ed[x+1]]-1ll;
return tmp+(cnt[x-1]+cnt[ed[x+1]]+1ll)*(cnt[x-1]+cnt[ed[x+1]]+1ll);
}else{
ll tmp=res-cnt[x-1]*cnt[x-1]-cnt[ed[x+1]]*cnt[ed[x+1]]-1ll;
return tmp+max((cnt[x-1]+1)*(cnt[x-1]+1)+cnt[ed[x+1]]*cnt[ed[x+1]],cnt[x-1]*cnt[x-1]+(cnt[ed[x+1]]+1ll)*(cnt[ed[x+1]]+1ll));
}
};
for(int i=1;i<=sz;++i){
if(ed[i]==i){
ans=max(ans,sol1(i));
}
else{
ans=max(ans,sol2(ed[i]));
if(i>1)ans=max(ans,sol3(i));
}
i=ed[i];
}
cout<<ans<<"\n";
return 0;
}
第3题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1e9+7;
std::vector<int> v;
ll n,k,ans;
map<int,int> cnt;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1,x;i<=n;++i){
cin>>x;
v.emplace_back(x);
}
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
for(auto it:v){
ans+=upper_bound(v.begin(),v.end(),k-it)-v.begin();
if(2ll*it<=k)ans-=1;
}
ans/=2;
ans%=mod;
cout<<ans;
return 0;
}
第4题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,x,a[100],k,cost;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>x;
for(int i=1;i<=n;++i)cin>>a[i];
cin>>k;
for(int i=1,x;i<=k;++i){
cin>>x;
cost+=a[x],a[x]=0ll;
}
if(cost>=x){
cout<<cost<<"\n";
}else{
bitset<(int)20*100000+99> vis;
vis[0]=1;
for(int i=1;i<=n;++i)vis|=(vis<<a[i]);
ll cz=x-cost;
for(int i=cz;i<=20*100000+98;++i)if(vis[i]){
cout<<i+cost<<"\n";
return 0;
}
}
return 0;
}