A:模拟
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
string s; cin>>s;
if(s[0]+s[1]+s[2]==s[3]+s[4]+s[5])cout<<"YES"<
B:贪心 取最小数计算所有差值
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n; cin>>n;
int a[55],mi=1e8;
for(int i=0;i>a[i];
mi=min(mi,a[i]);
}
ll ans=0;
for(int i=0;i
C:暴力枚举每对串,计算字符差值,取最小值
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n,m; cin>>n>>m;
string s[n+1];
for(int i=0;i>s[i];
int ans=2e8;
for(int i=0;i
D:枚举每个格子计算四个斜边方向的和,取最大值
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n,m; cin>>n>>m;
int a[205][205]={0};
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
ll ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ll temp=-3*a[i][j]; //后面加了4次
int x=i,y=j;
while(x&&y){
temp+=a[x][y];
x--;y--;
}
x=i,y=j;
while(x<=n&&y<=m){
temp+=a[x][y];
x++;y++;
}
x=i,y=j;
while(x&&y<=m){
temp+=a[x][y];
x--;y++;
}
x=i,y=j;
while(y&&x<=n){
temp+=a[x][y];
x++;y--;
}
ans=max(ans,temp);
}
cout<
E:前缀和二分
lower_bound 版
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1,greater<>()); //大到小排序
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; //前缀和
while(m--){
int x; cin>>x;
int k=lower_bound(sum+1,sum+n+1,x)-sum;
if(k>n)cout<<-1<
二分版:
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1,greater<>()); //大到小排序
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; //前缀和
while(m--){
int x; cin>>x;
int l=1,r=n+1;
while(l>1;
if(sum[mid]>=x)r=mid;
else l=mid+1;
}
//int k=lower_bound(sum+1,sum+n+1,x)-sum;
if(l>n)cout<<-1<
F:大到小排序,看是否连续且每个数的个数大于k,用map记录每个数的个数,输入x时,将x-1加入map便于判断是否连续,取答案区间的最大差值。
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n,k; cin>>n>>k;
int x;
mapmp;
for(int i=1;i<=n;i++)cin>>x,mp[x]++,mp[x-1]+=0;
int ansl=0,ansr=-1,l=-1,r=0;
for(auto i:mp){
if(i.yansr-ansl)ansr=i.x,ansl=l; //新区间的差值大于旧的最大值 更新最大值
}
if(ansr==-1)cout<<-1<
G:深搜计算每个节点及其子树的黑白色和
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<v[N];
int n,x,ans=0;
int b[N],w[N]; //自身及其子树的黑白色数量
string s;
void dfs(int u,int fa){ //当前节点,父结点
if(s[u-1]=='B')b[u]++;
else w[u]++;
for(auto i:v[u]){
if(i==fa) continue;
dfs(i,u);
b[u]+=b[i]; //当前节点加上所有子树的黑白色
w[u]+=w[i];
}
if(w[u]==b[u]) ans++;
}
int main(){
IC;
int t; cin>>t;
while(t--){
cin>>n;
memset(b,0,sizeof b);
memset(w,0,sizeof w);
for(int i=1;i<=n;i++) v[i].clear();
for(int i=2;i<=n;i++){
cin>>x;
v[x].push_back(i);
}
cin>>s;
ans=0;
dfs(1,1);
cout<
H1:暴力计算逆序对数与相等对数
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<>t;
while(t--){
int n; cin>>n;
int a[1005],ans=0;
for(int i=1;i<=n;i++){
cin>>a[i];
for(int j=1;j
H2:归并排序
#include
using namespace std;
#define ll long long
#define x first
#define y second
#define pii pair
#define ump unordered_map
#define IC std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug cout<<"++++++++++"<= r) return 0;
int mid = l + r >> 1;
ll res =merge_sort(q,l,mid)+merge_sort(q,mid+1,r);
int k =0,i=l,j=mid + 1;
while (i<=mid&&j<=r)
if (q[i]>t;
while(t--){
int n;cin>>n;
for(int i=0;i>a[i];
cout<