P14953 乒乓博弈
题意
小,小,小三个人玩乒乓球。他们计划玩局,第一局是小和小 b 玩,每局人之后,赢的人继续,另一个人代替输的,小想知道他最多能玩几局,最少能玩几局
思路
最多就是上来时候一直赢,最少就是一直换
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
int n;
void solve(){
cin >> n;
cout << n - 1<< ' '<< n / 2<< '\n';
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T737108 黑白棋子操作
题意
有一些棋子,要么是黑,要么是白,可以把相邻的黑白棋子交换位置,但是得是黑在左白在右,请问可以执行几次这个操作
思路
总次数就是看每个白色面前有几个黑色
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
const int N = 2e5 + 5;
int ans[N];
int res;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s;
for(int i = 0;i < s.size();i++){
if(s[i] == 'B'){
ans[i] = ans[i - 1] + 1;
}else{
ans[i] = ans[i - 1];
res += ans[i];
}
}
cout << res;
return 0;
}P2356 弹珠游戏
题意
有一种炮台,可以消灭所有本行本列的所有敌人,并获得每个敌人可获得的积分,请问把炮台放到哪可以使得积分最多,不能和敌人放到一起
思路
一个数组记录每行的值,一个数组记录每列的值,选择位置时的积分就是本行本列的值的和
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int a[N][N];
int n;
int ans = -1;
int col[N], wol[N];
int check(int x, int y){
int res = 0;
res += col[x];
res += wol[y];
return res;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> a[i][j];
col[i] += a[i][j];
wol[j] += a[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(a[i][j] == 0){
ans = max(ans, check(i ,j));
}
}
}
if(ans == -1){
cout << "Bad Game!";
}else{
cout << ans;
}
return 0;
}P15232 「CROI · R3」浣熊的快车道
题意
右条路,每条路要收元,有辆车,每辆车只允许自己这条路有辆车在,请问如何排列车子让收费最小
思路
优先考虑收费小的车道,优先考虑容忍度大的车子,一直放车子,知道放不了,换下一条路接着放,直到都放完所有车道,还有车子输出,否则输出价格
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
priority_queue <int, vector<int>, greater<int> > a;
priority_queue <int> b;
int ans;
int res;
int n, m;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
for(int i = 1;i <= n;i++){
int x;
cin >> x;
a.push(x);
}
for(int i = 1;i <= m;i++){
int x;
cin >> x;
b.push(x);
}
for(int i = 1;i <= n;i++){
int cnt = 0;
while(b.size() > 0 and b.top() > cnt){
b.pop();
cnt++;
res += a.top();
}
a.pop();
}
if(b.size() > 0){
cout << -1;
return 0;
}
cout << res;
return 0;
}P14361 [CSP-S 2025] 社团招新
题意
有人要加入编程队,编程队有三个部门,每个人对三个部门都有一个好感度,每个部门顶多有人,请问如何让三个部门的好感度综合最大
思路
记录好每次输入的最大值和最小值的差值,如果发现某个部门多了一些人,就剔除掉,知道人数刚好小于
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int t;
void solve(){
priority_queue <int, vector<int>, greater<int> > pq1;
priority_queue <int, vector<int>, greater<int> > pq2;
priority_queue <int, vector<int>, greater<int> > pq3;
cin >> n;
int s1 = 0, s2 = 0, s3 = 0;
for(int i = 1;i <= n;i++){
int x, y, z;
cin >> x>> y>> z;
if(x >= y and x >= z){
s1 += x;
if(y > z){
pq1.push(x - y);
}else{
pq1.push(x - z);
}
}else if(y >= x and y >= z){
s2 += y;
if(x > z){
pq2.push(y - x);
}else{
pq2.push(y - z);
}
}else if(z >= y and z >= x){
s3 += z;
if(y > x){
pq3.push(z - y);
}else{
pq3.push(z - x);
}
}
}
int ans = s1 + s2 + s3;
while(pq1.size() > n / 2){
ans -= pq1.top();
pq1.pop();
}
while(pq2.size() > n / 2){
ans -= pq2.top();
pq2.pop();
}
while(pq3.size() > n / 2){
ans -= pq3.top();
pq3.pop();
}
cout << ans<< '\n';
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}