首页 > 其他分享 >【刷题日记】其二

【刷题日记】其二

时间:2023-10-31 10:49:21浏览次数:28  
标签:cout -- res 其二 len forn 刷题 日记 define

2023.10.30 星期一

晚上有场div.2,刷刷思维题

17:21 R1400 思维 T00:25:41

https://codeforces.com/problemset/problem/1605/C
最后还是看题解了,自己做一直WA2,原来少了一种情况
反正就是最少的时候,只有两个a相邻和三个a相邻的情况,都枚举一遍就行

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll T;

int n;
string s;
void solve() {
	cin >> n >> s;
	s = ' ' + s;

	int ans = inf;
	int cnta, cntb, cntc;
	int l = 1;
	while (l < n) {//判断2个a
		if (s[l] == 'a') {
			cntb = cntc = 0;
			forn(i, l + 1, n) {
				if (s[i] == 'b') cntb++;
				if (s[i] == 'c') cntc++;
				if (s[i] == 'a') {
					if (cntb <= 1 && cntc <= 1) ans = min(ans, i - l + 1);
					break;
				}
			}
		}
		l++;
	}
	l = 1;
	while (l < n) {//判断3个a
		if (s[l] == 'a') {
			cnta = cntb = cntc = 0;
			forn(i, l + 1, n) {
				if (s[i] == 'b') cntb++;
				if (s[i] == 'c') cntc++;
				if (s[i] == 'a') cnta++;
				if (cnta == 2) {
					if (cntb <= 2 && cntc <= 2) ans = min(ans, i - l + 1);
					break;
				}
			}
		}
		l++;
	}

	if (ans == inf) ans = -1;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	// T=1;

	while (T--) {
		solve();
	}

	return 0;
}

18:15 R1400 思维 二进制 构造 T00:10:17

https://codeforces.com/problemset/problem/1095/C
这题挺简单的,直接贪,有两种情况肯定构造不出来
那么其他情况就可以,用一个队列存二进制数,如果总是小于k,就找一个不是1的拆开,直到总数为k

代码
// Problem: Powers Of Two
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1095C
// Memory Limit: 250 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll T;

ll n, k;
int bit[100];
int cnt;
void solve() {
	cin >> n >> k;

	int res = n;
	while (res > 0) {
		bit[++cnt] = res % 2;
		res /= 2;
	}
	int cnt1 = 0;
	forn(i, 1, cnt) if (bit[i] == 1) cnt1++;
	if (n < k || cnt1 > k) {
		cn;
		return;
	}
	cy;
	queue<int> q;
	ll x = 1;
	forn(i, 1, cnt) {
		if (bit[i] == 1) q.push(x);
		x *= 2;
	}
	while (q.size() < k) {
		x = q.front();
		q.pop();
		if (x != 1) {
			q.push(x / 2);
			q.push(x / 2);
		} else
			q.push(x);
	}

	while (!q.empty()) cout << q.front() << ' ', q.pop();
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	T = 1;

	while (T--) {
		solve();
	}

	return 0;
}

18:27 R1400 思维(妙) T00:07:57

https://codeforces.com/problemset/problem/1380/B
我直接贪,因为每一种都有可能,也就是说,对于我出的第i个,对战序列中的每一个,我只需要求出哪一种赢的最多,然后我每个都出这个就行了
(第一次忘了把计数清0了,白WA了一次)

代码
// Problem: Universal Solution
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1380B
// Memory Limit: 250 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll T;

int n;
string s;
void solve() {
	cin >> s;
	n = s.size();
	s = ' ' + s;

	int res, ma = 0;
	char x;
	res = 0;
	forn(i, 1, n) {
		if (s[i] == 'R') res++;
	}
	if (res > ma) {
		ma = res;
		x = 'P';
	}
	res = 0;
	forn(i, 1, n) {
		if (s[i] == 'S') res++;
	}
	if (res > ma) {
		ma = res;
		x = 'R';
	}
	res = 0;
	forn(i, 1, n) {
		if (s[i] == 'P') res++;
	}
	if (res > ma) {
		ma = res;
		x = 'S';
	}
	forn(i, 1, n) cout << x;
	cout << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	// T=1;

	while (T--) {
		solve();
	}

	return 0;
}

18:37 R1400 贪心 (byd蓝桥杯又出cf原题)

https://codeforces.com/problemset/problem/1197/C
https://www.lanqiao.cn/problems/5888/learning/?contest_id=145 (蓝桥杯原题)
这题也简单,原数组有序,拆成2段,把所有相邻的差加起来,找到两个相邻差最大的,然后减掉就行,剩下的就最小
同理k段也是,直接把相邻的差排序,然后减去最大的k-1个即可

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 3e5 + 5;
ll T;

ll n, k;
ll h[N];
ll a[N];
void solve() {
	cin >> n >> k;
	forn(i, 1, n) cin >> h[i];

	forn(i, 1, n - 1) a[i] = h[i + 1] - h[i];
	ll ans = 0;
	sort(a + 1, a + n);
	forn(i, 1, n - k) ans += a[i];
	cout << ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//cin >> T;
	T = 1;

	while (T--) {
		solve();
	}

	return 0;
}

19:08 R1400 贪心很简单 模拟细节多(WA了3次) T忘了记

https://codeforces.com/problemset/problem/363/C
贪心就是,先删连续3个及以上的
然后再判断是不是s[i - 1] == s[i] && s[i - 2] == s[i - 3] (注意可能字符串长度没有4,得特判下)

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll T;

string s, res;
void solve() {
	cin >> s;
	int n = s.size();
	s = ' ' + s;
	char l1, l2;
	res = s.substr(1, min(2, (int) s.size()));//注意长度可能都没有2,下面同理
	forn(i, 3, n) {//先删三个及以上连一起的
		l1 = s[i - 1], l2 = s[i - 2];
		if (l1 == l2 && l2 == s[i]) continue;
		res += s[i];
	}
	s = res;
	res.clear();

	res = s.substr(0, min(3, (int) s.size()));

	forn(i, 3, s.size() - 1) {//再删这样的
		if (s[i - 1] == s[i] && s[i - 2] == s[i - 3]) {
			s[i] = '?';
			continue;
		}
		res += s[i];
	}

	cout << res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	T = 1;

	while (T--) {
		solve();
	}

	return 0;
}

19:24 R1400 思维 T00:07:50

https://codeforces.com/problemset/problem/1251/B
一眼跟0和1的数量有关,而且还跟字符串长度有关
因为回文,如果长度为奇数就--,因为中间是什么斗无所谓
然后1和0的个数用到的肯定也是偶数,是奇数就--,保证构造时一定消耗偶数个
然后就是贪心,肯定先从长度小的开始构造,排序即可

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e3 + 5;
ll T;

int n;
string s[N];
int len[N];//字符串长度
int c0, c1;//0,1个数
void solve() {
	c0 = c1 = 0;
	cin >> n;
	forn(i, 1, n) {
		cin >> s[i];
		len[i] = s[i].size();
		if (len[i] % 2 == 1) len[i]--;
		for (auto j : s[i]) {
			if (j == '0') c0++;
			else
				c1++;
		}
	}

	if (c0 % 2 == 1) c0--;
	if (c1 % 2 == 1) c1--;
	int ans = 0;
	sort(len + 1, len + n + 1);
	forn(i, 1, n) {
		if (c0 >= len[i]) ans++, c0 -= len[i];
		else if (c1 >= len[i])
			ans++, c1 -= len[i];
		else {
			if (c0 + c1 >= len[i]) {
				len[i] -= c0;
				c0 = 0;
				c1 -= len[i];
				ans++;
			}
		}
	}

	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	// T=1;

	while (T--) {
		solve();
	}

	return 0;
}

19:39 R1400 思维 T00:09:41

https://codeforces.com/problemset/problem/1110/B
太水了
先假设只能贴一段,那么长度就是啊a[n]-a[1]+1,能再贴一段,相当于把这一整段切除一部分,变成两段胶带,直接找相差最大的就行了,推广到k段同理,相邻的距离都求出来,排序即可

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
ll T;

ll n, m, k;
ll ans;
ll a[N], b[N];
void solve() {
	cin >> n >> m >> k;
	forn(i, 1, n) cin >> a[i], b[i] = a[i] - a[i - 1] - 1;

	ans = a[n] - a[1] + 1;
	sort(b + 2, b + n + 1);
	int cnt = n;
	forn(i, 1, min(n - 1, k - 1)) ans -= b[cnt--];

	cout << ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	T = 1;

	while (T--) {
		solve();
	}

	return 0;
}

标签:cout,--,res,其二,len,forn,刷题,日记,define
From: https://www.cnblogs.com/ydc666/p/17798612.html

相关文章

  • NOIP 2023 考前学习日记
    前言属于是闲着没事干,看到自己去年写过一次,所以再写一次。10月23日(今日运势:大吉)上午:调联考异或,没调出来。消消乐,想想想。消消乐,写写写。消消乐,卡卡卡。消消乐,寄寄寄。详见记录:link。下午:模拟考试,什么神仙水题啊(T4没看),谔谔谔。消消乐,谢谢MLE教皇的Trie-Tree,过了......
  • 10.30日记
    当一个进程在等待永远不可能发生的事件时,就会产生死锁,若系统中多个进程出于死锁状态,就会造成系统死锁。 死锁产生的必要条件:   资源互斥   每个进程占有资源并等待其他资源   系统不能剥夺进程资源   进程资源图是一个环路 死锁产生后,解决措施是打破四大条件,有......
  • 10.29日记
    从下往上依次是:物理层,数据链路层,网络层,传输层,会话层,表示层,应用层。   物理层:二进制数据传输,物理链路和物理特性相关。   数据链路层:将数据封装成帧进行传输,准确传送至局域网内的物理主机上。   网络层:数据分组传输和路由选择,能准确的将数据传输到互联网的主机上。  ......
  • 刷题笔记——矩阵(C)
    85.最大矩形-力扣(LeetCode)给定一个仅包含 0 和 1 、大小为 rowsxcols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。解题思路依次遍历矩阵的每一行,计算每列落在的该行的”1“的个数,那么,本题就转换成了”柱状图的最大面积“。代码实现intlargestRectangleAr......
  • LeedCode刷题(2)-Java随机数练习
    2.随机数练习(1)随机生成数题目:请编写如下所示程序随机生成并显示一位数的正整数(1~9的值)随机生成并显示一位数的负整数(-9~-1的值)随机生成并显示两位数的正整数(10~99的值)①Random类总结random是Java提供的一个类库,它的实例会生成一连串的伪随机数Random创建实例有......
  • LeedCode刷题(1)-交替合并字符串
    1.交替合并字符串题目:给你两个字符串word1和word2。请你从word1开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并字符串的末尾。示例1:输入:word1="abc",word2="pqr"输出:"apbqcr"解释:字符串合并情况如下所示:word1:ab......
  • 十八籽 | 寒月廿七日记
    那一串十八籽,是班长送我的生日礼物,只是因为寒假帮他解决了几个问题,为了感谢我送的,好像不是什么贵重的东西。但是我就一直戴在手上,直到有一天它突然就断开了,被我永远留在了一个酒店的角落里。我从来没有给它拍过照,唯一的影像都是和我的手表一起出现在某张照片的角落,直到手表也换了......
  • 10.27日记
    能力成熟度模型CMM 能力成熟度模型CMM:对软件组织化阶段的描述,随着软件组织地定义、实施,测量、控制和改进其软件过程,软件组织地能力经过这些阶段逐步提高。   初始级(Initial):软件过程的特点是杂乱无章,又是甚至很混乱,几乎没有明确定义的步骤,项目的完成全依赖个人的努力和英雄式......
  • 踩坑日记,参数*位置是很重要的!!!
    踩坑日记,参数*位置是很重要的!!!classDome():def__init__(self):passdefverify(self,*args,name=None,**kwargs):print(name,args,kwargs)defverify2(self,name=None,*args,**kwargs):print(name,args,k......
  • 刷题记录 2023-10-26
    最近需要刷一点博弈论的题目LG-1288\(\Rightarrow\)题目链接可以想到,如果可操作序列的长度是奇数,那么先手必胜,如果是偶数,那么先手必败。LG-1290\(\Rightarrow\)题目链接设\(f(i,j)\)表示当前较大的石子堆和较小的石子堆的大小分别为\(i,j\),先手者是否存在必胜策略。可......