当前位置:   article > 正文

蓝桥杯省模拟赛_#pragma gcc optimize(2)

#pragma gcc optimize(2)

蓝桥杯省模拟赛

后面几题都自己暴力对拍了一下,可能还会有错大家可以参考。

第一题:

在这里插入图片描述
基本思路:

我们可以直接手算,或者求稳的话随便枚举一下所有情况就好了,下面是用的状态枚举。
答案: 14

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define ll long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
   
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) {
    if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) {
    x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
   
  if (x < 0) {
    putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

bool can(string s){
   
	int cnt = 0,n = s.size();
	for(int i = 0 ; i < n ; i++){
   
		if(s[i] == '(') cnt++;
		else{
   
			cnt--;
			if(cnt < 0) return false;
		}
	}
	return cnt == 0;
}
signed main() {
   
  IO;
  int ans = 0;
  for(int i = 0 ; i < (1 << 8) ; i++){
   
  	string str;
  	int cnt = 0;
  	for(int j = 0 ; j < 8 ; j ++){
   
  		if(i >> j & 1){
   
  		 str += '(';
  		 cnt++;
  		}else {
   
  			str += ')';
  		}
  	}
  	if(cnt != 4) continue;
  	if(can(str)) ans++;
  }
  cout << ans << '\n';
  return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
第二题:

在这里插入图片描述
这个没啥好说的边最少的无向连通图就是树,n-1条边
答案: 2018

第三题:

在这里插入图片描述
基本思路:

用全排列函数直接算一下字符串的全排列就好了,可以用set维护一下防止有重复情况。

答案; 2520

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define ll long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
   
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) {
    if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) {
    x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
   
  if (x < 0) {
    putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

signed main() {
   
	IO;
	string str = "LANQIAO";
	sort(str.begin(),str.end());
	set<string> st;
	do{
   
		st.insert(str);
	}while (next_permutation(str.begin(),str.end()));
	cout << st.size
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/374365
推荐阅读
相关标签
  

闽ICP备14008679号