A. Brain’s Photos(Codefoeces 707A)
思路
遍歷像素塊兒,如果發現彩色像素的話題給“圖片”就是彩色的,否則就是黑白的。
代碼
#include <bits/stdc++.h>
using namespace std;
bool ok = true;
char s[10];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for(int i = 0; i < n * m; i++) {
scanf("%s", s);
if(s[0] == 'C' || s[0] == 'M' || s[0] == 'Y') {
ok = false;
}
}
puts(ok ? "#Black&White" : "#Color");
return 0;
}
B. Bakery(Codeforces 707B)
思路
最優的情況下,選擇的烘培屋和貨源地一定是相鄰的(若不相鄰則一定能找到更優的情況),否則就無解。於是枚舉圖的邊,若當前邊所連兩點分別是烘焙屋和貨源地的話就用邊權更新最小值
代碼
#include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v, w;
edge() {}
edge(int u, int v, int w): u(u), v(v), w(w) {}
bool operator < (const edge& o) const {
return w < o.w;
}
};
const int maxn = 1e5 + 5, maxm = 1e5 + 5;
bool f[maxn];
int n, m, k, u, v, w, ans;
edge edges[maxm];
int main() {
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
edges[i] = edge(u, v, w);
}
while(k--) {
scanf("%d", &u);
f[u] = true;
}
ans = -1;
sort(edges, edges + m);
for(int i = 0; i < m; i++) {
edge e = edges[i];
if(f[e.u] != f[e.v]) {
ans = e.w;
break;
}
}
printf("%d\n", ans);
return 0;
}
C. Pythagorean Triples(Codeforces 707C)
思路
這題只要知道構造勾股數的方式就能解(限於本人的水平,就只能理解到這兒了)。
設
當
當
代碼
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
cin >> n;
if(n % 2 == 1) {
if(n == 1) {
cout << -1;
}
else {
n = (n - 1) / 2;
cout << 2 * n * n + 2 * n << ' ' << 2 * n * n + 2 * n + 1;
}
}
else {
if(n == 2) {
cout << -1;
}
else {
n = n / 2;
cout << n * n - 1 << ' ' << n * n + 1;
}
}
cout << endl;
return 0;
}
D. Persistent Bookcase(Codeforces 707D)
思路
本題的棘手之處在於題目中的第4個操作。在不能改變時間的情況下,狀態只能隨着時間的推進而轉移(一維)。現在可以改變時間,那麼狀態的轉移目的地就不唯一了,也就是說狀態的轉移會出現分叉。那麼狀態的轉移的圖示就從一個數軸變成了一棵樹(二維)。如果我們能夠構造出這棵樹,就能在用
令詢問
當然,要在狀態轉移的過程中計算出書本的數量也並非十分容易的事。關鍵就在於怎麼表示狀態。設時刻
代碼
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010, maxq = 1e5 + 5;
bool inv[maxn], B[maxn][maxn];
int n, m, q, k, res;
int o[maxq], a[maxq], b[maxq], ans[maxq], num[maxn];
vector <int> G[maxq];
void dfs(int u) {
ans[u] = res;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i], x = a[v], y = b[v];
int pnum = num[x];
int pinv = inv[x];
int pB = B[x][y];
int pres = res;
if(o[v] == 1 && B[x][y] ^ inv[x] == 0) {
B[x][y] ^= 1;
num[x]++;
res++;
}
if(o[v] == 2 && B[x][y] ^ inv[x] == 1) {
B[x][y] ^= 1;
num[x]--;
res--;
}
if(o[v] == 3) {
res += m - 2 * num[x];
num[x] = m - num[x];
inv[x] ^= 1;
}
dfs(v);
num[x] = pnum;
inv[x] = pinv;
B[x][y] = pB;
res = pres;
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for(int i = 1; i <= q; i++) {
scanf("%d", &o[i]);
if(o[i] <= 2) {
scanf("%d%d", &a[i], &b[i]);
G[i-1].push_back(i);
}
else if(o[i] == 3) {
scanf("%d", &a[i]);
G[i-1].push_back(i);
}
else {
scanf("%d", &k);
G[k].push_back(i);
}
}
dfs(0);
for(int i = 1; i <= q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
(其它題目略)