题意:
给定序列中是否存在“hello”这个子序列
解题思路:
O(n)扫一遍,逐个判断是否存在
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100 + 5;
int main()
{
char s[maxn];
while (cin>>s) {
int len = strlen(s);
int Count = 0;
for (int i=0; i<len; ++i) {
if (Count == 0 && s[i] == 'h') {
++Count;
}
if (Count == 1 && s[i] == 'e') {
++Count;
}
if (Count == 3 && s[i] == 'l') {
++Count;
}
if (Count == 2 && s[i] == 'l') {
++Count;
}
if (Count == 4 && s[i] == 'o') {
++Count;
}
if (Count == 5) {
break;
}
}
if (Count == 5) {
cout<<"YES"<<endl;
} else {
cout<<"NO"<<endl;
}
}
return 0;
}
2、HDU 5630 Rikka with Chess
题意:
一个 n * m 的布满棋子的棋盘,只有黑白两种颜色,且相邻的棋子的颜色不一样
现在每次可以选择一个子矩形将其中所有棋子的颜色取反
问将棋盘上所有棋子的颜色都变成一样的最少操作次数
解题思路:
假设所有奇数行偶数列和偶数行奇数列的棋子都为黑色,其余都为白色
那么只需要将偶数行的棋子的颜色进行取反,然后再将偶数列的棋子的颜色取反
就可以以最少的操作次数将棋盘上所有棋子都变为白色
#include <iostream>
using namespace std;
int main()
{
int T;
while (cin>>T) {
int n, m;
while (T--) {
cin>>n>>m;
cout<<n/2+m/2<<endl;
}
}
return 0;
}
3、Codeforces_631A Interview
题意:
对于数组 x,f(x, L, R) = xL | x(L + 1) | … | xR
给出两个长度为 n 的数组 a,b(0 ≤ ai ≤ 10^9,0 ≤ bi ≤ 10^9)
找出两个整数 L,R (1 ≤ L ≤ R ≤ n)使得 f(a, L, R) + f(b, L, R) 最大
最后输出最大值
解题思路:
a | b >= a(a ≥ 0,b ≥ 0)
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n;
while (cin>>n) {
int i;
__int64 num1 = 0, num2 = 0;
int a[1010];
int b[1010];
for (i=0; i<n; ++i) {
cin>>a[i];
num1 |= a[i];
}
for (i=0; i<n; ++i) {
cin>>b[i];
num2 |= b[i];
}
cout<<num1+num2<<endl;
}
return 0;
}
4、Codeforces_629A Far Relative’s Birthday Cake
题意:
一个 n*n 的正方形蛋糕是由许多个边长为 1 的正方形小块蛋糕组成
有些小块蛋糕上有巧克力
每一行和每一列的开心值为有巧克力的蛋糕块的对数
每一对只能被计数一次
求总的开心值
解题思路:
对于每块有巧克力的蛋糕,只将它与同一行左边和同一列上边的有巧克力的蛋糕配对
复杂度O(n^3),但是 n 只有 1e3 ,所以想到啥写啥,懒得去再写O(n^2)的做法了,毕竟题有点水(虽然我也很水)
#include <iostream>
using namespace std;
int main()
{
int n;
char s[110][110];
while (cin>>n) {
int i, j, k;
long long num = 0;
for (i=0; i<n; ++i) {
for (j=0; j<n; ++j) {
cin>>s[i][j];
if (s[i][j] == 'C') {
for (k=0; k<j; ++k) {
if (s[i][k] == 'C') {
++num;
}
}
for (k=0; k<i; ++k) {
if (s[k][j] == 'C') {
++num;
}
}
}
}
}
cout<<num<<endl;
}
return 0;
}
5、 Codeforces_628A Tennis Tournament
题意:
n 个人两两进行比赛,只要输一次就会被淘汰,最后未淘汰的人数为 1 时结束比赛
每场比赛每名选手需要 b 瓶水,裁判需要 1 瓶水,为每名选手提供 p 条毛巾
解题思路:
比赛进行了 n - 1 场
#include <iostream>
using namespace std;
int main()
{
int n, b, q;
while (cin>>n>>b>>q) {
int x, y;
x = (n-1)*(2*b+1);
y = n*q;
cout<<x<<" "<<y<<endl;
}
return 0;
}
6、CodeForces_3A Shortest path of the king
题意:
给定起点和终点,每一步有 8 种走法,问最少几步能从起点走到终点
然后打印路径
解题思路:
1、根据终点对应起点的方位走
2、bfs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
int dir[][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
int x1, x2, y_1, y2;
bool vis[10][10];
int pre[100][100];
struct Node {
int x, y;
int Count;
};
int bfs(void);
int main()
{
char s[5], e[5];
cin >> s >> e;
x1 = 8 - (s[1] - '0');
x2 = 8 - (e[1] - '0');
y_1 = s[0] - 'a';
y2 = e[0] - 'a';
printf("%d\n", max(abs(x1-x2), abs(y_1-y2)));
if (x1 >= x2 && y_1 >= y2) { //终点在起点的左上
while (!(x1 == x2 && y_1 == y2)) {
if (x1 > x2 && y_1 > y2) {
printf("LU\n");
--x1;
--y_1;
} else if (x1 > x2) {
printf("U\n");
--x1;
} else {
printf("L\n");
--y_1;
}
}
} else if (x1 >= x2 && y_1 <= y2) { //终点在起点的右上
while (!(x1 == x2 && y_1 == y2)) {
if (x1 > x2 && y_1 < y2) {
printf("RU\n");
--x1;
++y_1;
} else if (x1 > x2) {
printf("U\n");
--x1;
} else {
printf("R\n");
++y_1;
}
}
} else if (x1 <= x2 && y_1 >= y2) { //终点在起点的左下
while (!(x1 == x2 && y_1 == y2)) {
if (x1 < x2 && y_1 > y2) {
printf("LD\n");
++x1;
--y_1;
} else if (x1 < x2) {
printf("D\n");
++x1;
} else {
printf("L\n");
--y_1;
}
}
} else if (x1 <= x2 && y_1 <= y2) { //终点在起点的右下
while (!(x1 == x2 && y_1 == y2)) {
if (x1 < x2 && y_1 < y2) {
printf("RD\n");
++x1;
++y_1;
} else if (x1 < x2) {
printf("D\n");
++x1;
} else {
printf("R\n");
++y_1;
}
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
int dir[][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
int x1, x2, y_1, y2;
bool vis[10][10];
int pre[100][100];
struct Node {
int x, y;
int Count;
};
int bfs(void);
int main()
{
char s[5], e[5];
cin >> s >> e;
x1 = 8 - (s[1] - '0');
x2 = 8 - (e[1] - '0');
y_1 = s[0] - 'a';
y2 = e[0] - 'a';
printf("%d\n", bfs());
int x = x2, y = y2;
stack<string> S;
while (pre[x][y] != x*8 + y) {
int nx = pre[x][y] / 8;
int ny = pre[x][y] % 8;
string t;
if (ny < y) {
t += 'R';
}
if (ny > y) {
t += 'L';
}
if (nx > x) {
t += 'U';
}
if (nx < x) {
t += 'D';
}
S.push(t);
x = nx;
y = ny;
}
while (!S.empty()) {
cout << S.top() << endl;
S.pop();
}
return 0;
}
int bfs(void)
{
memset(vis, false, sizeof(vis));
Node node;
queue<Node> Q;
node.x = x1;
node.y = y_1;
node.Count = 0;
Q.push(node);
vis[x1][y_1] = true;
pre[x1][y_1] = x1*8 + y_1;
while (!Q.empty()) {
int nx = Q.front().x;
int ny = Q.front().y;
int t = nx*8 + ny;
int nCount = Q.front().Count;
if (nx == x2 && ny == y2) {
return nCount;
}
Q.pop();
for (int i = 0; i < 8; ++i) {
node.x = nx + dir[i][0];
node.y = ny + dir[i][1];
node.Count = nCount + 1;
if (0 <= node.x && node.x <= 8 && 0 <= node.y && node.y <= 8 && !vis[node.x][node.y]) {
vis[node.x][node.y] = true;
Q.push(node);
pre[node.x][node.y] = t;
}
}
}
return 0;
}
题意:
判断一个数是否可以分解成 2 个非 0 的偶数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
int main()
{
int w;
scanf("%d", &w);
w -= 2;
if (w != 0 && (w&1) == 0) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
8、CodeForces_676A Nicholas and Permutation
题意:
n 个不同的整数 a1, a2, …, an (1 ≤ ai ≤ n)
把其中两个整数进行一次交换,使得 1 和 n 的距离最大
求这个最大的距离
解题思路:
将不改变 1 和 n 的位置、改变 1 的位置和改变 n 的位置后的三个距离进行比较,取最大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int a[maxn];
int main()
{
int n;
scanf("%d", &n);
int Min_pos, Max_pos;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
if (a[i] == 1) {
Min_pos = i;
}
if (a[i] == n) {
Max_pos = i;
}
}
if (Min_pos > Max_pos) {
swap(Min_pos, Max_pos);
}
int Max = Max_pos - Min_pos;
Max = max(Max, (Max_pos - 1));
Max = max(Max, (n - Min_pos));
printf("%d\n", Max);
return 0;
}
9、CodeForces_677A Vanya and Fence
题意:
n 个人沿着高度为 h 的围墙走
为了不被警卫发现,比围墙高的人需要弯着腰
假设正常走路的人的宽度为 1,弯着腰走路的人的宽度为 2
问使他们不被发现的道路的最小的宽度
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const double eps = 1e-8;
const int INF = 0x7fffffff;
const int maxn = 1000 + 10;
int a[maxn];
int main()
{
int n, h;
scanf("%d%d", &n, &h);
int sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
if (a[i] > h) {
sum += 2;
} else {
sum += 1;
}
}
printf("%d\n", sum);
return 0;
}
10、CodeForces_677B Vanya and Food Processor
题意:
给出 n 个土豆的高度、加工机器的高度和机器每秒钟可以粉碎的高度
问按顺序将这些土豆粉碎完需要多少时间
解题思路:
模拟
注意:开 long long 是一个好习惯,没开 long long 被 Hack 好桑心…
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const double eps = 1e-8;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int a[maxn];
int main()
{
int n, h, k;
scanf("%d%d%d", &n, &h, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
ll Count = 0;
int leave = 0;
int sum;
for (int i = 0; i < n; ++i) {
if (a[i] + leave > h) {
++Count;
leave = 0;
--i;
} else {
sum = a[i] + leave;
int j = i + 1;
while (1) {
if (j >= n) {
break;
}
if (sum + a[j] > h) {
break;
}
sum += a[j];
++j;
++i;
}
if (sum < k) {
++Count;
leave = 0;
} else {
Count += (sum/k);
leave = sum % k;
}
}
}
if (leave != 0) {
++Count;
}
printf("%I64d\n", Count);
return 0;
}