首页 > 其他分享 >luoguP4907 A换B problem

luoguP4907 A换B problem

时间:2024-08-29 16:28:20浏览次数:2  
标签:luoguP4907 LL 卡时 else ans return problem include

数据有点水,加个卡时就可以过。

代码加注释送上:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime>
#define LL long long
using namespace std;
LL read() {
	LL k = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9')
		k = k * 10 + c - 48, c = getchar();
	return k * f;
}
bool tong[5][100]; //tong[花色][点数]
struct zzz {
	int c, k;
}a[100]; //c:花色,k:点数
bool flag;
int n, ans = 100, ans2 = 100, kti;
bool judge() { //判断是否为顺子
	int num = 0; bool flag2 = 1;
	for(int i = 1; i <= 4; ++i) {
		int minn = 100, maxn = 0;
		for(int j = 1; j <= 13; ++j) {
			if(tong[i][j] && minn == 100) minn = j;
			if(tong[i][j]) maxn = max(maxn, j);
		}
		for(int j = minn; j <= maxn; ++j)
			if(!tong[i][j]) flag2 = 0, ++num;
	}
	ans2 = min(ans2, num);
	return flag2;
}
void dfs(int pos, int tot) {
	if(tot >= ans) return ;
	if(judge()) {
		ans = min(ans, tot);
		return ;
	}
	if(pos > n) return ;
	//=====卡时
	if((clock() - kti) * 1000 >= 980 * CLOCKS_PER_SEC){
    	if(ans <= 52) printf("Yes\n%d", ans);
		else printf("No\n%d", ans2);
		exit(0);
    }
    //=====
	for(int i = 1; i <= 4; ++i) {
		if(tong[i][a[pos].k] && i != a[pos].c) continue;
		tong[a[pos].c][a[pos].k] = 0;
		tong[i][a[pos].k] = 1;
		if(i != a[pos].c) dfs(pos+1, tot+1);
		else dfs(pos+1, tot);
		tong[i][a[pos].k] = 0, tong[a[pos].c][a[pos].k] = 1;
	}
}
int main() {
	kti = clock(), n = read();
	for(int i = 1; i <= n; ++i) {
		a[i].c = read();
		char c[2]; cin >> c;
		if(c[0] == 'A') a[i].k = 1;
		else if(c[0] == 'J') a[i].k = 11;
		else if(c[0] == 'Q') a[i].k = 12;
		else if(c[0] == 'K') a[i].k = 13;
		else if(c[0] == '1') a[i].k = 10;
		else a[i].k = c[0] - '0';
	}
	for(int i = 1; i <= n; ++i) tong[a[i].c][a[i].k] = 1;
	dfs(1, 0);
	if(ans <= 52) printf("Yes\n%d", ans);
	else printf("No\n%d", ans2);
	return 0;
}


标签:luoguP4907,LL,卡时,else,ans,return,problem,include
From: https://www.cnblogs.com/mcr130102/p/18386933

相关文章

  • D2. Turtle and a MEX Problem (Hard Version)
    D2.TurtleandaMEXProblem(HardVersion)Thetwoversionsaredifferentproblems.Inthisversionoftheproblem,youcan'tchoosethesameintegertwiceormore.Youcanmakehacksonlyifbothversionsaresolved.Oneday,Turtlewasplayingwit......
  • a+b problem
    题目描述输入两个整数a,ba,b,输出它们的和(∣a∣,∣b∣≤109∣a∣,∣b∣≤109)。注意Pascal使用integer会爆掉哦!有负数哦!C/C++的main函数必须是int类型,而且C最后要return0。这不仅对洛谷其他题目有效,而且也是NOIP/CSP/NOI比赛的要求!好吧,同志们,我们就从这一题开......
  • A+B Problem
    异或运算对加法不满足分配律mod(2^32)可以视为保留二进制表示下的32位大胆猜测解是唯一的点击查看代码#include<bits/stdc++.h>usingnamespacestd;unsignedinta[300005],b[300005],ans[300005];intmain(){ ios::sync_with_stdio(false); cin.tie(0); intT; ......
  • 041 Time to Practice Dynamic Styling - Problem
    示例index.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>VueBa......
  • Problems in Mathematical Analysis (American First Edition)
    https://www.amazon.com/Problems-Mathematical-Analysis-Boris-Demidovich/dp/B0007BNL1W/ ProblemsinMathematicalAnalysis(AmericanFirstEdition) Reviews:https://www.amazon.com/gp/customer-reviews/R225A0UUURIG9B/ref=cm_cr_dp_d_rvw_ttl?ie=UTF8&ASI......
  • E. Not a Nim Problem
    E.NotaNimProblemTwoplayers,AliceandBob,areplayingagame.Theyhave$n$pilesofstones,withthe$i$-thpileinitiallycontaining$a_i$stones.Ontheirturn,aplayercanchooseanypileofstonesandtakeanypositivenumberofstonesfromi......
  • 题解:AT_arc181_b [ARC181B] Annoying String Problem
    思路首先我们可以根据两个字符串算出另外一个字符串\(T\)的长度。算出来之后,因为我们要满足等式两边完全相等,所以很容易得出这两个字符串应该都是由一些公共的字串拼接而成的。设\(S\)串中最小的周期为\(P\)。所以应该满足\(|P|\Large{\mid}\normalsize\gcd(|S|,|T|)\)......
  • ChatGPT Is a Knowledgeable but Inexperienced Solver: An Investigation of Commons
    文章目录题目摘要简介什么是常识GPT能否有效回答常识问题?GPT是否知道回答问题的常识性知识?GPT是否具备常识性知识?GPT能否有效利用语境中的常识进行推理?相关工作结论与讨论题目ChatGPT是一个知识渊博但缺乏经验的解决者:对大型语言模型中常识问题的调查论文地......
  • D. Another Problem About Dividing Numbers
    原题链接题意有两个数\(a,b\)每次可以拿走其中一个数的若干个质因子,请问恰好\(k\)次操作后能否使\(a=b\)分析假设\(a,b\)最后到达的是\(c\),那么\(\frac{a}{c}\)的质因子个数加上\(\frac{b}{c}\)的质因子个数一定大于等于\(k\)(为什么可以大于?因为一次操作可以多......
  • P1001 A+B Problem(整活-dijstra堆优化)
    OK啊,这就是普通的\(a+b\)嘛这是一道十分淼的题目,乍一看,这不就是dijstra堆优化的模板题吗?首先,建立三个节点,两条线行,OK开始Code#include<bits/stdc++.h>usingnamespacestd;constlonglongN=99999,M=999999;typedefpair<longlong,longlong>PII;priority_......