ABC 407

A:水
B:简单概率题,直接暴力统计
C:按规则还原为0即可。
D:剪枝的一道非常好的题,要尽可能剪到位才能过,数据也给的比较合适,

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include <sstream>
#include <vector>
using namespace std;
const double eps = 1e-10;
long long num[25][25],ansmax = -1;
bool mark[25][25];
int H,W;

void DDFS(int limt,int lastx,int lasty){
	if(limt == 0){
		long long tpp = 0;
		for(int i=1;i<=H;i++){
			for(int j=1;j<=W;j++){
				if(mark[i][j] == 0){
					tpp = tpp xor num[i][j];
				}
			}
		}
		if(tpp > ansmax){
			ansmax = tpp;
		}
		return;
	}
	for(int i=lastx;i<=H;i++){
		int fk;
		if(i == lastx){
			fk = lasty;
		}else{
			fk = 1;
		}
		for(int j=fk;j<=W;j++){
			if(mark[i][j] == 0){
				if(i+1 <= H && mark[i+1][j] == 0){
					mark[i][j] = 1;
					mark[i+1][j] = 1;
					DDFS(limt - 1,i,j);
					mark[i][j] = 0;
					mark[i+1][j] = 0;
				}
				if(j+1 <= W && mark[i][j+1] == 0){
					mark[i][j] = 1;
					mark[i][j+1] = 1;
					DDFS(limt - 1,i,j);
					mark[i][j] = 0;
					mark[i][j+1] = 0;
				}
			}
		}
	}
	return ;
}
int main() {
	int kuai = 0;
	scanf("%d %d",&H,&W);
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			scanf("%lld",&num[i][j]);
		}
	}
	kuai = H*W/2;
	for(int i = 0;i <= kuai;i++){
		DDFS(i,1,1);
	}
	printf("%lld",ansmax);
	return 0;
}
/*
  4 5
  1 2 3 4 5
  6 5 4 1 2
  8 5 2 4 6
  8 6 1 2 3
 */

E:首先复习如何判断合法的括号序列,常用方法之一是使用栈的原理
然后是如何生成随机的合法括号序列:只需要保证任意处往前的左括号数永远多于右括号数且整个序列左右括号数相等即可
然后没做出来
下面分析题解:用优先堆维护前i位里的最大值,当i为奇数时选择当前最大值为左括号。由于是每隔1个选一次左括号,所以必然能保证前i个里的左括号数恒大于等于右括号数且i为偶数时左右括号数必然相等。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=15<<26;

int main(){
	
	std::ifstream in("text.txt");
	std::cin.rdbuf(in.rdbuf());
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int Q;
	scanf("%d",&Q);
	while(Q--){
		ll N,x,ans = 0;
		scanf("%lld",&N);
		priority_queue<ll> PQ;
		for(int i=1;i<=N+N;i++){
			scanf("%lld",&x);
			PQ.push(x);
			if(i%2==1){
				ans+=PQ.top();
				PQ.pop();
			}
		}
		printf("%lld\n",ans);
	}
}

F:看了几天没看懂,但是发现一个数据可以检测出某些不严谨算法
5
5 8 8 8 1
G:
end

posted @ 2025-05-27 20:35  Thomastine  阅读(44)  评论(0)    收藏  举报