每日三题
9.27
思路:快速幂
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; double qsm(double a,int b){ double res=(double)1; while(b){ if(b&1)res*=a; b>>=1; a*=a; } return res; } void solve(){ int n,t;cin>>n>>t; double ans,p=1.000000011; ans=(double)n*qsm(p,t); cout<<fixed<<setprecision(20)<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:若sum能整除n,那么把所有数变为sum/n;否则,有sum%n个数变为sum/n+1,那么将大于sum/n+1的数变为sum/n+1即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int s=0,n;cin>>n; vector<int>ve(n); for(int i=0;i<n;++i){ cin>>ve[i];s+=ve[i]; } int ans=0,b=0,a=s/n; if(s%n){ b=s%n; sort(ve.begin(),ve.end()); for(int i=n-1;i>=0;--i){ if(b){ ans+=abs(ve[i]-a-1);b--; }else ans+=abs(ve[i]-a); } } else{ for(int i=0;i<n;++i){ ans+=abs(ve[i]-a); } } cout<<ans/2; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:可以把每个区间的端点的位置以及是左/右端点 统计起来,按位置前、左端点在前排序;sum统计经过的端点数,经过左端点sum++,右端点sum--,当sum=k时,无论是经过左还是右端点,都是一段有效区间答案的端点,都统计到答案里
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; struct E{ int st,op;//10 bool operator<(const E&e)const{ if(st==e.st)return op>e.op; return st<e.st; } }; void solve(){ int k,n;cin>>n>>k; vector<E>ve(2*n); for(int i=0,p=0;i<n;++i){ cin>>ve[p].st>>ve[p+1].st; ve[p++].op=1,ve[p++].op=0; } sort(ve.begin(),ve.end()); vector<int>ans; for(int i=0,s=0;i<2*n;++i){ if(ve[i].op){ s++; if(s==k)ans.push_back(ve[i].st); }else{ if(s==k)ans.push_back(ve[i].st); s--; } } cout<<ans.size()/2<<'\n'; for(int i=0;i<ans.size();i+=2){ cout<<ans[i]<<' '<<ans[i+1]<<'\n'; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
9.28
思路:由于是找满足条件的子串,对于最后一位是0、4、8的数,只要前面组成的数为偶数,即前一位为偶数,一定可以与前面所有的数组成满足条件的数;对于最后一位是2、6的数,只要前面组成的数为奇数,即前一位为奇数,一定可以与前面所有数组成满足条件的数。
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ string s;cin>>s; int n=s.size(),ans; s=" "+s; vector<int>f(n+1); if((s[1]-'0')%4==0)f[1]=1; ans=f[1]; for(int i=2;i<=n;++i){ if(s[i]=='0'||s[i]=='4'||s[i]=='8'){ if((s[i-1]-'0')%2==0)f[i]+=i-1; f[i]++; }else if(s[i]=='2'||s[i]=='6'){ if((s[i-1]-'0')%2)f[i]+=i-1; } ans+=f[i]; } cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
B - Replace To Make Regular Bracket Sequence
思路:用栈存左边,遇到右边判断下是否相同即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ string s;cin>>s; stack<char>st; map<char,int>mp; mp['(']=mp[')']=1; mp['[']=mp[']']=2; mp['{']=mp['}']=3; mp['<']=mp['>']=4; int ans=0; for(int i=0;i<s.size();++i){ if(s[i]=='<'||s[i]=='('||s[i]=='{'||s[i]=='['){ st.push(s[i]); }else{ if(st.empty()){ cout<<"Impossible";return ; } if(mp[st.top()]!=mp[s[i]]){ ans++; } st.pop(); } } if(st.empty())cout<<ans; else cout<<"Impossible"; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:dfs把联通的'.'个数求出来,并给这个连通块编号。每个'*'遍历四个方向的'.'连通块即可(答案要%10!)
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; string s[N]; int st[N][N],sum,vis[N*N]; int ans[N][N],n,m,idx,cnt[N*N]; int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; void dfs(int x,int y){ for(int i=0;i<4;++i){ int xx=x+dx[i],yy=y+dy[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&st[xx][yy]==0&&s[xx][yy]=='.'){ st[xx][yy]=idx; sum++; dfs(xx,yy); } } } void solve(){ cin>>n>>m; for(int i=1;i<=n;++i){ cin>>s[i]; s[i]=" "+s[i]; } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ if(st[i][j]==0&&s[i][j]=='.'){ sum=1; st[i][j]=++idx; dfs(i,j); cnt[idx]=sum; } } } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ if(s[i][j]=='.')cout<<'.'; else { int c=1; for(int k=0;k<4;++k){ int x=i+dx[k],y=j+dy[k]; if(x>=1&&x<=n&&y>=1&&y<=m&&vis[st[x][y]]!=(n*(i-1)+j)){ vis[st[x][y]]=(n*(i-1)+j); c+=cnt[st[x][y]]; } } cout<<c%10; } }cout<<'\n'; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
9.29
思路:取每个长度为k的连续段的和,求平均值
#include<bits/stdc++.h> using namespace std; #define int long long #define PII pair<int,int> const int mod=1e9+7; void solve(){ int n,k;cin>>n>>k; int s=0; vector<int>ve(n+1); for(int i=1;i<=n;++i){ cin>>ve[i];ve[i]+=ve[i-1]; } for(int i=0;i<=n-k;++i){ s+=ve[i+k]-ve[i]; } double ans=(double)s/(n-k+1); cout<<fixed<<setprecision(9)<<ans<<'\n'; } signed main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int _=1; //cin>>_; while (_--) solve(); }
思路:第一次的h1、h2为第一次下课见到的高度,白天+黑夜算做一天,由于从白天开始,且白天是上升,那么第一次晚上后算作第一天开始,计算出一天内上升的高度,求需要多少天即可;
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int h1,h2,a,b;cin>>h1>>h2>>a>>b; h1+=8*a; if(h1>=h2)cout<<0; else{ int c=h2-h1,d=12*(a-b); if(d<=0)cout<<-1; else cout<<(c+d-1)/d; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:用双指针维护一下即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,k;cin>>n>>k; vector<int>ve(n+1); for(int i=0;i<n;++i)cin>>ve[i]; int ma=0,l,r; map<int,int>mp; for(int i=0,j=0;i<n;++i){ if(mp.size()<k)mp[ve[i]]++; else{ if(mp[ve[i]]==0){ mp[ve[j]]--; while(mp[ve[j]]!=0&&j+1<=i) { mp[ve[++j]]--; } j++; }mp[ve[i]]++; } if(i-j+1>ma){ ma=i-j+1; l=j,r=i; } } cout<<l+1<<' '<<r+1; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
9.30
思路:维护前后最近的0的位置即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n;cin>>n; vector<int>ve(n+1),l(n+5),r(n+5); for(int i=1,last=0;i<=n;++i){ cin>>ve[i]; if(ve[i]==0)last=i; else{ if(last)l[i]=i-last; else l[i]=INF; } } for(int i=n,last=0;i>=1;--i){ if(ve[i]==0)last=i; else{ if(last)r[i]=last-i; else r[i]=INF; } } for(int i=1;i<=n;++i){ cout<<min(l[i],r[i])<<' '; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:模拟下过程即可(题目给的是每个位置的物品,每次加的是该物品的位置)
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,k,m,ans=0;cin>>n>>m>>k; vector<int>ve(k+1),pos(k+1); for(int i=1;i<=k;++i)cin>>ve[i],pos[ve[i]]=i; for(int i=1;i<=n;++i){ for(int j=1,x;j<=m;++j){ cin>>x; int p=pos[x]; ans+=p; for(int v=p;v>1;--v){ ve[v]=ve[v-1]; pos[ve[v]]=v; } ve[1]=x; pos[x]=1; } } cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:可以维护前缀pre,表示前面第一个不同的数的位置;也可以用数据结构维护区间最大和最小值及位置;
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,m;cin>>n>>m; vector<int>ve(n+1),pre(n+1); for(int i=1;i<=n;++i){ cin>>ve[i]; if(i==1)pre[i]=0; else{ if(ve[i]!=ve[i-1])pre[i]=i-1; else pre[i]=pre[i-1]; } } for(int i=1,l,r,x;i<=m;++i){ cin>>l>>r>>x; if(ve[r]!=x)cout<<r<<'\n'; else if(pre[r]<l)cout<<-1<<'\n'; else cout<<pre[r]<<'\n'; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
10.1
思路:判断x,y轴转换需要的次数是否同奇或同偶
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int x1,y1,x2,y2,x,y; cin>>x1>>y1>>x2>>y2>>x>>y; int c1=abs(x1-x2),c2=abs(y1-y2); if(c1%x||c2%y)cout<<"NO"; else{ c1/=x,c2/=y; if(c1%2==c2%2)cout<<"YES"; else cout<<"NO"; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:定为最中间的点最优
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n;cin>>n; vector<int>ve(n+1); for(int i=1;i<=n;++i)cin>>ve[i]; sort(ve.begin()+1,ve.end()); if(n%2)cout<<ve[n/2+1]; else cout<<ve[n/2]; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:dp,维护以i结尾有多少个合法区间,维护下i前的第一个pair的左端点即可知道以i结尾的合法区间有多少个,那么记录每对的右端点的最近的左端点
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,m;cin>>n>>m; vector<int>ve(n+1),pos(n+1),las(n+1); for(int i=1;i<=n;++i){ cin>>ve[i]; pos[ve[i]]=i; } for(int i=1,x,y;i<=m;++i){ cin>>x>>y; if(pos[x]>pos[y])swap(x,y); las[y]=max(las[y],pos[x]); } int ans=0,pre=0; for(int i=1;i<=n;++i){ if(las[ve[i]])pre=max(pre,las[ve[i]]); ans+=i-pre; } cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
10.2
思路:模拟每一步即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int st=1,n,k;cin>>n>>k; vector<int>ve(k+1),f(n+1); for(int i=1;i<=n;++i)f[i]=i; for(int i=1;i<=k;++i)cin>>ve[i]; for(int i=1;i<=k;++i){ int p=ve[i]+st; int now=p%n; if(now==0)now=n; cout<<f[now]<<' '; n--; if(now!=n+1) for(int j=now;j<=n;++j)f[j]=f[j+1]; else now=1; st=now; // for(int j=1;j<=n;++j)cout<<f[j]<<' ';cout<<'\n'; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:由于n为奇数,且要满足条件每一行/列/对角线的和为奇数,则每一行/列/对角线的奇数个数为奇数个,且总奇数数有一半,那么从第一行起每一行奇数个数为1、3、5...,在中心且对称,如下图
00100 01110 11111 01110 00100
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,ou=2,ji=1;cin>>n; int m=(n+1)/2; for(int i=1,l,r;i<=n;++i){ if(i<=m)l=m-i+1,r=m+i-1; else l=i-m+1,r=n-i+m; for(int j=1;j<=n;++j){ if(j>=l&&j<=r)cout<<ji,ji+=2; else cout<<ou,ou+=2; cout<<' '; }cout<<'\n'; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
思路:可以求以i结尾的最长子串,那么就要求出以i结尾最多能换的0的个数,可以二分求,用前缀和判断,也可以用队列预处理出来。
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=1e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,k;cin>>n>>k; vector<int>ve(n+1),f(n+1),las(n+1); queue<int>q; for(int i=1,l=0;i<=n;++i){ cin>>ve[i]; las[i]=l; if(ve[i]==0)q.push(i),l=i; if(q.size()>k)q.pop(); if(q.size())f[i]=q.front(); } // for(int i=1;i<=n;++i)cout<<f[i]<<' ';cout<<'\n'; int ma=0,ansl,ansr; for(int i=1,l;i<=n;++i){ int c=0; if(f[i])c=i-las[f[i]]; else if(ve[i]) c=i-las[i]; if(c>ma){ ma=c; ansl=i-c+1,ansr=i; }//cout<<c<<' '; } cout<<ma<<'\n'; for(int i=1;i<=n;++i){ if(i>=ansl&&i<=ansr)cout<<1<<' '; else cout<<ve[i]<<' '; } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
10.3
思路:对于连续相同的一段字母,若长度为奇数,从第二个开始每隔一个改变;若为偶数,从第一个开始每隔一个改变,可以保证这样的次数最少
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ string s;cin>>s; for(int i=0,r,j;i<s.size();++i){ r=i; while(r+1<s.size()&&s[r+1]==s[i])r++; char c=s[i]; while(c==s[i]||(r<s.size()-1&&c==s[r+1])||(i&&s[i-1]==c)){ if(c=='z')c='a'; else c++; } if((r-i+1)%2)j=i+1; else j=i; for(;j<=r;j+=2)s[j]=c; i=r; } cout<<s; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
思路:奇+奇=偶,奇+偶=奇;对于所有整偶数直接加即可,将所有整奇数加了之后,若奇数个数为偶数,需要减去一个最小的正奇数或加上一个最大的负奇数
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n;cin>>n; int ans=0,cnt1=0,mi=INF,ma=-INF; for(int x,i=0;i<n;++i){ cin>>x; if(x>=0){ if(x%2)ans+=x,cnt1++,mi=min(mi,x); else ans+=x; }else if(x%2)ma=max(ma,x); } if(cnt1%2==0)ans-=min(mi,abs(ma)); cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
思路:若区间i包含区间j,需满足li<=lj和rj<=ri;先按左端点排序(右端点也可)后满足了一个条件;对于第i个区间,第i个后的所有区间中满足rj<=ri的区间个数为第i区间包含的区间,可以用树状数组单点加及区间和来求;由于r范围较大,离散化后便可用树状数组求
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; struct E{ int l,r,id; }; int n; vector<int>f(N); int lowbit(int x){return x&-x;} int getsum(int x){ int ans=0; while(x>0){ ans+=f[x]; x-=lowbit(x); } return ans; } void add(int x,int k){ while(x<=n){ f[x]+=k; x+=lowbit(x); } } bool cmp1(E a,E b){return a.l<b.l;} bool cmp2(E a,E b){return a.r<b.r;} void solve(){ cin>>n; vector<E>ve(n+1); for(int i=1;i<=n;++i)cin>>ve[i].l>>ve[i].r,ve[i].id=i; sort(ve.begin()+1,ve.end(),cmp1); for(int i=1;i<=n;++i)ve[i].l=i; sort(ve.begin()+1,ve.end(),cmp2); vector<int>ans(n+1); for(int i=1;i<=n;++i){ ans[ve[i].id]=i-1- getsum(ve[i].l-1); add(ve[i].l,1); } for(int i=1;i<=n;++i)cout<<ans[i]<<'\n'; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
10.4
思路:1和任何正整数都互质,若存在不满足的插入1即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n;cin>>n; vector<int>ve(n),ans; for(int i=0;i<n;++i){ cin>>ve[i]; } for(int i=0;i<n-1;++i){ ans.push_back(ve[i]); if(__gcd(ve[i],ve[i+1])!=1)ans.push_back(1); }ans.push_back(ve[n-1]); cout<<ans.size()-n<<'\n'; for(auto v:ans)cout<<v<<' '; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
思路:倒的茶要单调,先倒一半,剩余的从高开始补满,还剩多的或不够倒即不存在
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,w,s=0;cin>>n>>w; vector<PII>a(n+1); for(int i=1;i<=n;++i)cin>>a[i].first,a[i].second=i,s+=(a[i].first+1)/2; if(s>w){ cout<<-1;return ; } sort(a.begin()+1,a.end()); int l=w-s; vector<int>ans(n+1); for(int i=n;i&&l;--i){ int c=min(a[i].first/2,l); l-=c; ans[a[i].second]=(a[i].first+1)/2+c; } if(l){ cout<<-1;return ; } for(int i=1;i<=n;++i){ if(ans[a[i].second]==0)ans[a[i].second]=(a[i].first+1)/2; } for(int i=1;i<=n;++i)cout<<ans[i]<<' '; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
思路:由于奇+奇=偶+偶=偶,那么数组里最多有一个奇数和一个偶数,特别的,1可以存在多个(1+1=2,2为素数);
那么就有两种情况:多个1和一个偶,一个奇和一个偶;取长度最多的情况
若找不到和为素数的情况,则任意一个数都可以
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; int st[N],primes[N],idx; void init(){ for(int i=2;i<N;++i){ if(!st[i]) primes[idx++]=i; for(int j=0;primes[j]*i<N;++j){ st[primes[j]*i]=1; if(i%primes[j]==0)break; } } } void solve(){ int n;cin>>n; int cnt1=0; vector<int>ve(n),ji,ou; int ok1=0; for(int i=0;i<n;++i){ cin>>ve[i]; if(ve[i]%2)ji.push_back(ve[i]); else { ou.push_back(ve[i]); if(!st[ve[i]+1])ok1=ve[i]; } if(ve[i]==1)cnt1++; } vector<int>ans; if(cnt1){ for(int i=1;i<=cnt1;++i)ans.push_back(1); if(ok1)ans.push_back(ok1); } if(ans.size()<=1){ for(int i=0;i<ji.size();++i){ for(int j=0;j<ou.size();++j){ if(!st[ji[i]+ou[j]]){ ans.clear(); ans.push_back(ji[i]),ans.push_back(ou[j]); break; } } } } if(ans.size()==0)ans.push_back(ve[0]); cout<<ans.size()<<'\n'; for(auto v:ans)cout<<v<<' '; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; init(); while(T--){ solve(); } return 0; }
10.5
思路:由于最后剩0个,且每次买一半,倒推回去即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,p;cin>>n>>p; int ans=0; vector<string>ve(n); for(int i=0;i<n;++i)cin>>ve[i]; for(int s=0,i=n-1;i>=0;--i){ if(ve[i]=="halfplus")s=s*2+1; else s*=2; ans+=s*10/2; } ans=ans/10*p+p/2*(ans%10!=0); cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
思路:记录下t串每个字符的个数,对于'?'按顺序凑满每个t即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ string s,t;cin>>s>>t; map<char,int>mp,f; for(int i=0;i<t.size();++i)mp[t[i]]++; for(int i=0;i<s.size();++i){ if(s[i]!='?'&&mp[s[i]]>0)f[s[i]]++; } for(int i=0,j=0;i<s.size();++i){ if(s[i]=='?'){ while(f[t[j]]>=mp[t[j]]){ if(j+1==t.size()){ j=0; for(auto &v:f){ v.second=max(0ll,v.second-mp[v.first]); } }else j++; } f[t[j]]++; s[i]=t[j]; } } cout<<s; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
思路:涂红色的是a的倍数,蓝色是b的倍数,对与lcm(a,b)的倍数两个颜色都能涂,那么涂价值最高的即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,a,b,aa,bb;cin>>n>>a>>b>>aa>>bb; int c=a*b/__gcd(a,b); int ac=n/a,bc=n/b,cc=n/c; if(aa>=bb)bc-=cc; else ac-=cc; cout<<ac*aa+bc*bb; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
10.6
思路:后两位能整除4的一定可以,枚举每个最后一位;还可以发现最后一位为0、4、8的只要前一位是偶数或最后一位是2、6的只要前一位是奇数也一定满足
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ string s;cin>>s; int ans=0; for(int i=0;i<s.size();++i){ if(s[i]=='0'||s[i]=='4'||s[i]=='8'){ if(i&&(s[i-1]-'0')%2==0)ans+=i; ans++; }else if(s[i]=='2'||s[i]=='6'){ if(i&&(s[i-1]-'0')%2)ans+=i; } } cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
思路:枚举每对矩形,判断是否能装下即可
#include<bits/stdc++.h> using namespace std; #define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve(){ int n,a,b,ans=0;cin>>n>>a>>b; vector<PII>ve(n); for(int i=0;i<n;++i){ cin>>ve[i].first>>ve[i].second; } for(int i=0;i<n;++i){ for(int j=0;j<i;++j){ int x1=ve[i].first+ve[j].first,y1=max(ve[i].second,ve[j].second); int x2=ve[i].first+ve[j].second,y2=max(ve[i].second,ve[j].first); int x3=ve[i].second+ve[j].first,y3=max(ve[i].first,ve[j].second); int x4=ve[i].second+ve[j].second,y4=max(ve[i].first,ve[j].first); if(min(x1,y1)<=min(a,b)&&max(x1,y1)<=max(a,b))ans=max(ans,ve[i].first*ve[i].second+ve[j].first*ve[j].second); else if(min(x2,y2)<=min(a,b)&&max(x2,y2)<=max(a,b))ans=max(ans,ve[i].first*ve[i].second+ve[j].first*ve[j].second); else if(min(x3,y3)<=min(a,b)&&max(x3,y3)<=max(a,b))ans=max(ans,ve[i].first*ve[i].second+ve[j].first*ve[j].second); else if(min(x4,y4)<=min(a,b)&&max(x4,y4)<=max(a,b))ans=max(ans,ve[i].first*ve[i].second+ve[j].first*ve[j].second); } } cout<<ans; } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
思路:可以求出2016.1.1的星期,若n小于2016,那么先倒回去找到n的星期,再暴力往后枚举即可
#include<bits/stdc++.h> using namespace std; //#define int long long //#define int __int128 #define double long double typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e6+5,INF=0x3f3f3f3f,Mod=1e9+7,mod=998244353; const double eps=1e-6; void solve() { int n; cin >> n; int p = 5, x , sx; if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0)sx = 366; else sx = 365; if (n == 2016)x = p; if (n < 2016) { for (int i = 2015;; --i) { int s = 365; if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)s = 366; int c=(s-p+1)%7; if(c==0)c=7; p = 8 - c; if (i == n) { if(p==7)p=0; x = p; break; } } } int l=min(2016,n); for (int i = l;; ++i) { int s = 365; if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)s = 366; p = (s + p) % 7; if (i + 1 == n)x = p; else if (i + 1 > n) { int ss=365; if (((i + 1) % 4 == 0 && (i + 1) % 100 != 0) || (i + 1) % 400 == 0)ss = 366; if (p == x && ss == sx) { cout << i + 1; break; } } } } signed main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; //cin>>T; // init(); while(T--){ solve(); } return 0; }
10.7