P4702 取石子
题意
Alice与Bob取石子,他们每次可以在一堆里拿一颗,谁拿到最后一颗谁赢。
解法
如果数组 的总和是偶数,那么bob一定会拿到最后一颗,也就是会bob会赢。相反的,如果数组 的总和是奇数,那么Alice会拿到最后一颗,Alice会赢。
注意: 会爆 所以得开 。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[110];
int sum;
signed main() {
int n;
cin >> n;
for(int i = 1;i <= n;i ++) {
cin >> a[i];
sum += a[i];
}
if(sum % 2 == 1) cout << "Alice";
else cout << "Bob";
return 0;
}P2945 [USACO09MAR] Sand Castle S
题意
Farmer John 要修改沙堡上 个垛堞的高度,每个垛堞原高度为 ,目标高度为 (可以排列)。增加单位高度的成本为 ,减少单位高度的成本为 。求最低修改成本。
解法
将原高度数组 和目标高度数组 分别排序。对于排序后相同位置的元素,如果原高度大于目标高度,则需要减少,成本为高度差乘以 ;如果原高度小于目标高度,则需要增加,成本为高度差乘以 。
#include<bits/stdc++.h>
using namespace std;
int a[100010],b[100010];
int ans;
signed main() {
int n,x,y;
cin >> n >> x >> y;
for(int i = 1;i <= n;i ++) {
cin >> a[i] >> b[i];
}
sort(a + 1,a + n + 1);
sort(b + 1,b + n + 1);
for(int i = 1;i <= n;i ++) {
if(a[i] >= b[i]) {
ans += abs(b[i] - a[i]) * y;
}else {
ans += abs(a[i] - b[i]) * x;
}
}
cout << ans;
return 0;
}B3829 [NICA #2] 字符串入门题
题意
有一个字符串,需要把它切成至少 段。从第二段开始,每一段都必须是前面所有段拼起来之后的子串。也就是说,后面的每一段都要在前面已经出现的部分里能找到。
解法
前面切一大段,后面每个字单独切一段。但是要保证后面每个字都在前面那段里出现过。
嗯对具体的:
先看看字符串最后 个字,检查这些字是不是都在前 个字里出现过。如果都出现过,就可以这样切:第一段是前 个字,后面 段每段一个字。如果有任何一个字没在前面出现过,那就没办法这样切,输出 。
#include<bits/stdc++.h>
using namespace std;
int vis[10010];//标记是否出现过
int main() {
int n,k;
cin >> n >> k;
string s;
cin >> s;
for(int i = 0;i <= n - k;i ++) vis[s[i]] = 1;
for(int i = n - k + 1;i < n;i ++) {
if(!vis[s[i]]) {
cout << -1;
return 0;
}
}
cout << k << endl;
for(int i = 0;i <= n - k;i ++) cout << s[i];
cout << endl;
for(int i = n - k + 1;i < n;i ++) cout << s[i] << endl;
}P1683 入门
题意
黄药师在桃花岛入口修了一条用瓷砖铺成的小路,有些瓷砖安全(.),有些瓷砖危险(#)。你从第一块砖(@)开始走,只能走到相邻的安全瓷砖上。问最多能走过多少块瓷砖(包括起点)。
解法
用 从起点开始遍历所有能到达的安全瓷砖。每走到一块新的安全瓷砖就计数加一,最后输出总计数。
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
};
int vis[1005][1005];
int n, m, cnt;
char g[1005][1005];
int d[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
void bfs(int sx, int sy) {
queue<node> q;
q.push({sx, sy});
cnt++;
vis[sx][sy] = 1;
while (!q.empty()) {
node t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = t.x + d[i][0];
int ny = t.y + d[i][1];
if (nx < 1 || nx > m || ny < 1 || ny > n) continue;
if (g[nx][ny] == '#') continue;
if (vis[nx][ny]) continue;
q.push({nx, ny});
vis[nx][ny] = 1;
cnt++;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int sx, sy;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> g[i][j];
if (g[i][j] == '@') {
sx = i;
sy = j;
}
}
}
bfs(sx, sy);
cout << cnt;
return 0;
}P9853 [入门赛 #17] 方程求解
题意
有 个方程,每个方程形如 或 ,需要求出每个方程的正整数解。然后有 次询问,每次问在 范围内有多少个不同的 是至少一个方程的解。
解法
先解每个方程,把解出来的 值存起来。因为可能有重复的解,所以要去重。对于每次询问,用二分查找找出在 范围内的解有多少个。
#include<bits/stdc++.h>
using namespace std;
long long a[200010],cnt;
int main() {
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) {
long long x,y,z;
char p,o,e;
cin>>x>>p>>o>>y>>e>>z;
if(o=='+') {
a[++cnt]=(z-y)/x;
}else{
a[++cnt]=(z+y)/x;
}
}
sort(a+1,a+cnt+1);
int sz=unique(a+1,a+cnt+1)-a-1;
while(q--) {
long long l,r;
cin>>l>>r;
int L=lower_bound(a+1,a+sz+1,l)-a-1;
int R=upper_bound(a+1,a+sz+1,r)-a-1;
cout<<R-L<<"\n";
}
return 0;
}