#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
int m;
cin >> n >> m;
ll ans = 0;
ll p = 0; // p (previous) 代表前一天工作的员工数
ll c = 0; // c (current) 代表当天需要工作的员工数
if (m > 0) {
cin >> p; // 读取第一天的数据
}
for (int i = 1; i < m; ++i) {
cin >> c;
// 如果前一天和当天需要的人数总和超过总员工数
// 就必须有人连续工作
if (p + c > n) {
ans += (p + c - n);
}
p = c; // 更新前一天的人数,为下一次迭代做准备
}
cout << ans << endl;
return 0;
}
用一个布尔数组 h (has) 来标记每个同学是否拥有糖果。h 的大小为 N+1,初始化为 false。
遍历所有 K 种糖果的归属信息,对于每个拥有糖果的同学 p,将其标记 h[p] = true。
最后,遍历从 1 到 N 的所有同学,统计 h[i] 仍然为 false 的同学数量,即为答案。
这个方法的时间复杂度为 O(N+∑di),空间复杂度为 O(N),对于本题数据范围来说非常高效。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void slv() {
int n, k;
cin >> n >> k;
// h[i] 标记同学i是否拥有糖果
vector<bool> h(n + 1, false);
for (int i = 0; i < k; ++i) {
int d;
cin >> d;
for (int j = 0; j < d; ++j) {
int p;
cin >> p;
h[p] = true;
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (!h[i]) {
ans++;
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
slv();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s, all;
while (getline(cin, s))
{
if (!s.empty() && s.back() == '#')
{ // 终止行
all += s.substr(0, s.size() - 1);
break;
}
all += s + '\n'; // 保留换行
}
auto isAlnum = [](char c) { return isalpha(c) || isdigit(c); };
int cnt = 0, n = all.size();
for (int i = 0; i + 3 < n; ++i)
{
if (tolower(all[i]) == 'y' &&
tolower(all[i+1]) == 'y' &&
tolower(all[i+2]) == 'l' &&
tolower(all[i+3]) == 'x') {
bool left = (i-1 >= 0 && isAlnum(all[i-1]));
bool right = (i+4 < n && isAlnum(all[i+4]));
if (!left && !right) ++cnt;
}
}
cout << cnt << '\n';
return 0;
}
2. 公共子序列答案判定
思路来源
判定一个字符串 ans 是否既是 X 与 Y 的公共子序列,且长度等于给定 l。
公共子序列判定 ➜ 双指针扫描O(∣X∣+∣Y∣+∣ans∣)。
需要维护的信息
两个指针 i, j 走 X, Y。
扫描 ans 时分别在 X,Y 找下一个匹配字符。
细节点 & 坑
不必验证老师给的 l 真的是 LCS 长度,只需比较学生答案长度是否等于 l。
字符串允许到1000,单次扫描完全足够。
学生提交串可能 比 l 长、包含非字母 等,这些都要判错。
代码
#include <bits/stdc++.h>
using namespace std;
bool check(string s, string t)
{ // s 是否为 t 的子序列
size_t i = 0;
for (char c : t) if (i < s.size() && s[i] == c) ++i;
return i == s.size();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, l; cin >> n >> l;
string X, Y;
cin >> X >> Y;
string ans;
while (n--)
{
cin >> ans;
if (int(ans.size()) == l && check(ans, X) && check(ans, Y))
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
3. AI 助教 Token 统计
思路来源
模拟整场对话,每轮 问→答:
提问行的 token 暂存;只有等到对应 AI 回答出现时,这部分 token 才并入“输入”计数。
回答行的 token 即刻加入“输出”计数。
Token 定义:连写英文字母视作一个 token;单独标点视作一个 token;空格忽略。
细节点 & 坑
数据保证“问答交替”,无需额外配对判断。
多空格、标点相邻等情况要正确分割。
如果对话以 学生行结束(没等到回答),最后那句不算输入。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int cntTok(string s){ // 统计一行 Token 数
int n = s.size(), t = 0;
for(int i = 0; i < n; ){
if(isalpha(s[i])){
++ t;
while(i < n && isalpha(s[i])) ++ i;
}else if(s[i] == ' ') ++ i;
else { ++ t; ++ i; }
}
return t;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s;
int p = 0;
int in = 0, out = 0; // 结果
vector<string> words;
while(getline(cin ,s), s.find("#") == -1)
words.push_back(s);
for(int i = 0;i < words.size(); ++ i)
{
string l = words[i];
if(l.size() >= 2 && l[0] == '#' && l[1] == ':') break;
char r = l[0]; // 角色
int tok = cntTok(l.substr(2));
p += tok;// 前缀和
if(r == 'S')
{
if(i + 1 != words.size()) in += p;
}
else
{
out += tok;
}
}
cout << in << ' ' << out << '\n';
return 0;
}
4. 核桃数判定
题目太水了,不想写思路了,进制转换判断是否出现过单独的一段000 或者111即可,直接放个AC代码
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
signed main()
{
int n;
for(cin >> n; n --;)
{
unsigned int x;
cin >> x;
int p = x & 1, c = 1;
bool ok = 0;
x
很明显的一个贪心问题,考虑将 b 中比 a 中武力值强的替换过去即可(b 中最强的替换 a 中最弱的)。
需要注意只能招募 k 次,所以招募次数用完即便还有也不要招募了。
另外还有招募不到 k 次,这个时候 b 中已经没有比 a 中武力值强的了,这个时候也不能继续招募了。
💻 C++ 14 标准程序
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, k, a[N], b[N];
inline void solve() {
cin >> n >> k;
int Sum = 0;
for (int i = 1; i <= n; i++) cin >> a[i], Sum += a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);
int L = 1, R = n;
while (k--) {
if (a[L] < b[R]) {
Sum += (b[R] - a[L]);
L++; R--;
} else break;
}
printf("%d\n", Sum);
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
LL n, k;
inline void solve() {
cin >> n >> k;
LL Sum = (1 + k) * k / 2;
if (n < Sum) printf("No\n");
else printf("Yes\n");
}
int main() {
int _ = 1;
while (_--) solve();
return 0;
}
📌 题目名称:C.不稳定的魔力补给
🔍 题目描述
在穿越魔法裂隙的尽头,噜噜与杨乐多来到了一座古老的魔法祭坛。祭坛上摆放着 n 个蓝瓶,每个蓝瓶中蕴藏着神秘的魔力。
输入平台编号范围很小,直接用 int 足矣,累加时间也不会溢出(最大约 100×6×100+5×100<105)。
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
int cur = 0;
long long t = 0;
while(N--){
int x;
cin >> x;
if(x > cur) t += 6LL * (x - cur);
else if(x < cur) t += 4LL * (cur - x);
t += 5; // 处理事务停留
cur = x;
}
cout << t << "\n";
return 0;
}
浮点精度:f 是小数,直接 ceil(tot/f) 可能因精度误差会挂分。
建议把 f 用分子分母表示:
f=1010+2L⟹d=⌈10+2L10×tot⌉.
当 V==0 时 L=0,公式依然适用。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while(T--){
ll G,Y,M,S;
int V,L;
cin >> G >> Y >> M >> S >> V >> L;
ll tot = G*64 + Y*16 + M*4 + S;
// f = (10 + 2*L) / 10
ll num = 10 + 2LL*L; // 分子
ll den = 10; // 分母
if(V==0) num = den; // f =1
// 求 d = ceil(tot / f) = ceil(tot * den / num)
ll d = (tot*den + num - 1) / num;
cout << d << "\n";
}
return 0;
}
#include<bits/stdc++.h>
#define int long long
using namespace std;
int qp(int a, int b, int p) {
int res = 1;
while(b) {
if(b & 1) res = res * a % p;
b >>= 1;
a = a * a % p;
}
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, p;
cin >> a >> b >> p;
cout << qp(a, b, p);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 200005;
ll a[MAXN]; // 羊价
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; ll m;
i