201409-1
解题思路:数据量小,直接暴力枚举
参考代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
int a[N];
int ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (abs(a[i] - a[j]) == 1) ans++;
}
}
cout << ans;
return 0;
}
201409-2
解题思路:直接暴力,连差分都用不上
参考代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
int g[105][105];
int ans;
int main() {
cin >> n;
int x1, y1, x2, y2;
for (int i = 1; i <= n; i++) {
cin >> x1 >> y1 >> x2 >> y2;
for (int x = x1; x < x2; x++) {
for (int y = y1; y < y2; y++) {
g[x][y] = 1;
}
}
}
for (int i = 0; i <= 100; i++) {
for (int j = 0; j <= 100; j++) {
if (g[i][j]) ans++;
}
}
cout << ans;
return 0;
}
201409-3
解题思路:暴力匹配甚至不需要KMP
参考代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
string s, x, y;
int n, is;
bool check(string y) {
int len = s.size();
if (y.size() < len) return 0;
for (int i = 0; i + len - 1 < y.size(); i++) {
if (y.substr(i, len) == s) return 1;
}
return 0;
}
int main() {
cin >> s >> is >> n;
if (is == 0) {
for (int i = 0; i < s.size(); i++) {
if (s[i] <= 'Z' && s[i] >= 'A') s[i] -= 'A' - 'a';
}
}
for (int i = 1; i <= n; i++) {
cin >> x;
y = x;
if (is == 0) {
for (int j = 0; j < y.size(); j++) {
if (y[j] <= 'Z' && y[j] >= 'A') y[j] -= 'A' - 'a';
}
}
if (check(y)) cout << x << endl;
}
return 0;
}
201409-4
解题思路:把分店的位置都放到队列里,BFS求最短路就行了
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int t[N][N];
bool is[N][N];
int n, m, k, d;
struct point {
int x, y, w;
} g[N * N];
ll ans;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
t[i][j] = N * N;
}
}
}
queue<point>q;
void bfs() {
while (!q.empty()) {
point now = q.front();
q.pop();
int tw = now.w + 1;
for (int i = 0; i < 4; i++) {
int tx = now.x + dx[i];
int ty = now.y + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n) {
if (is[tx][ty] == 0 && tw < t[tx][ty]) {
t[tx][ty] = tw;
q.push({tx, ty, tw});
}
}
}
}
}
int main() {
cin >> n >> m >> k >> d;
init();
int x, y;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
q.push({x, y, 0});
t[x][y] = 0;
}
for (int i = 1; i <= k; i++) {
cin >> g[i].x >> g[i].y >> g[i].w;
}
for (int i = 1; i <= d; i++) {
cin >> x >> y;
is[x][y] = 1;
}
bfs();
for (int i = 1; i <= k; i++) {
ans += g[i].w * t[g[i].x][g[i].y];
}
cout << ans;
return 0;
}
201409-5
解题思路:
参考代码