A题:
小B准备出模拟赛。
她把题目按难度分为四等,分值分别为6,7,8,9。
已知小B共出了m道题,共n分。
求小B最少出了多少道6分题。
则满足6m <= n <= 9m,设出6分题数x,则7(m-x) <= n - 6x <= 9(m-x)
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,m;
cin>>n>>m;
assert(max(n,m)<=1e12);
if(n<6*m||n>9*m)puts("jgzjgzjgz");
else cout<<max(0LL,7*m-n);
}
D:
小B喜欢美食。
现在有n个美食排成一排摆在小B的面前,依次编号为1…n,编号为i的食物大小为 a[i] ,即足够小B吃 a[i] 口。
小B每次会吃两口,这两口要么是编号相同的美食,要么是编号之差的绝对值为1的美食。
小B想知道,她最多能吃几次?
贪心策略:对i-th食物,a[i]/=2,若还剩下1,则和i+1-th食物一起吃一口
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 3;
int n;
ll a[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
ll ans = 0;
for(int i = 0; i < n; i++) {
ans += a[i] / 2;
if((a[i] & 1) && a[i + 1] > 0) ans++, a[i + 1]--;
}
cout << ans << endl;
}
G
求a|(a+1)|(a+2)|…|(b-1)|b
若a=b,则答案即为a。
考虑a和b的二进制表示从高到低第一个不同的位i,必定b的第i位=1,a的第i位=0。那么可以断定,对于答案的二进制表示,(1) 比第i位更高的那些位一定跟a相同,(2) 第i位及比第i位更低的那些位一定为1。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 3;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll a, b;
while(cin >> a >> b) {
ll x = 1, pos = a ^ b;
while(x <= pos) x *= 2;
x--;
cout << (a | b | x) << endl;
}
}
J:
你在一个 n 行 m 列的网格迷宫中,迷宫的每一格要么为空,要么有一个障碍。
你当前在第 r 行第 c 列(保证该格子为空)。每次移动你可以向上下左右任意一个方向移动一格,前提是不能走到障碍上,也不能超出迷宫的边界。
你向左移动的次数不能超过 x 次,向右不能超过 y 次。
问在这种情况下,对于每个格子,是否存在一种移动方案让你走到它。
输出有多少个格子存在移动方案让你走到它。
按题意到每个格子有向左移动的步数l,向右移动的步数r,其中格子与初始格子的列数差即是r-l=a,我们记录到每个格子的最小l+r=b,则跑出可达的格子的状态后计算出l和r进行判断即可。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 3;
const int inf = 0x3f3f3f3f;
int n, m, r, c, x, y;
char maze[maxn][maxn];
deque<int> q;
int g[maxn][maxn];
void bfs() {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) g[i][j] = inf;
g[r][c] = 0;
q.push_back(r * maxn + c);
while(!q.empty()) {
int x = q.front(); q.pop_front();
int y = x % maxn; x /= maxn;
if(maze[x - 1][y] == '.' && g[x - 1][y] > g[x][y]) {
g[x - 1][y] = g[x][y];
q.push_front((x - 1) * maxn + y);
}
if(maze[x + 1][y] == '.' && g[x + 1][y] > g[x][y]) {
g[x + 1][y] = g[x][y];
q.push_front((x + 1) * maxn + y);
}
if(maze[x][y - 1] == '.' && g[x][y - 1] > g[x][y] + 1) {
g[x][y - 1] = g[x][y] + 1;
q.push_back(x * maxn + y - 1);
}
if(maze[x][y + 1] == '.' && g[x][y + 1] > g[x][y] + 1) {
g[x][y + 1] = g[x][y] + 1;
q.push_back(x * maxn + y + 1);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> r >> c >> x >> y;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) cin >> maze[i][j];
bfs();
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
if(g[i][j] != inf) {
int a = j - c, b = g[i][j];
ans += (b - a) / 2 <= x && (b + a) / 2 <= y;
}
}
cout << ans << endl;
}