题解
由于是整列整列的处理的,所以相当于处理一个数组,然后取相反数,排个序处理就可以了
代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MAXN = 2e2 + 5;
const int INF = 0x3f3f3f3f;
int S[MAXN];
int N;
int main(){
while(~scanf("%d", &N)){
int x, sum = 0;
memset(S, 0, sizeof(S));
for(int i = 1;i <= N;i ++){
for(int j = 1;j <= N;j ++){
scanf("%d", &x);
sum += x;
S[j] += x;
}
}
sort(S + 1, S + N + 1);
for(int i = 1;i <= N;i += 2){
if(S[i] > 0 || S[i] < 0 && i + 1 <= N && S[i + 1] > 0 && S[i + 1] >= - S[i]) break;
if(i + 1 > N) break;
sum += - 2 * S[i + 1] - 2 * S[i];
}
printf("%d\n", sum);
}
return 0;
}
题解
先判断是不是树:为一棵树不是深林,然后不能成环
判断是不是二叉树:子节点要小于等于 2
判断是不是排序二叉树:如果子树的所有节点大于当前节点则是有子树,否则为左子树,然后不满足条件就判断出错
代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int N;
struct Node {
vector<int> info;
} Nsc[MAXN], Nsp[MAXN];
struct NDE {
int left, right;
} nde[MAXN];
pair<int, int> piia[MAXN];
int par[MAXN];
char path[MAXN << 4];
int pos;
int sz;
bool fg;
int _find(int x) {
return x == par[x] ? x : par[x] = _find(par[x]);
}
void unit(int a, int b) {
a = _find(a);
b = _find(b);
if(a == b) return;
par[a] = b;
sz --;
}
void init() {
for(int i = 0; i < MAXN; i ++) {
par[i] = i;
}
}
// min, max
pair<int, int> DFS1(int p) {
if(p == -1) return make_pair(p, p);
pair<int, int> piil = DFS1(nde[p].left);
pair<int, int> piir = DFS1(nde[p].right);
if(piil.first != -1 && piir.first != -1 && piil.first > piir.first ||
piil.first > p && piir.first == -1 ||
piil.first == -1 && piir.second < p) {
swap(piil, piir);
swap(nde[p].left, nde[p].right);
}
if(piil.first == -1 && piir.second == -1 ||
piil.first == -1 && piir.first > p ||
piil.second < p && piir.first > p ||
piil.second < p && piir.first == -1) {
} else {
fg = true;
}
int minv = min((piil.first != -1 ? piil.first : p), (piir.first != -1 ? piir.first : p));
int maxv = max((piil.second != -1 ? piil.second : p), (piir.second != -1 ? piir.second : p));
return make_pair(min(p, minv), max(p, maxv));
}
void DFS(int p) {
if(p == -1) {
path[pos ++] = '(';
path[pos ++] = ')';
return;
}
path[pos ++] = '(';
char buf[10];
sprintf(buf, "%d", p);
int len = strlen(buf);
for(int i = 0; i < len; i ++) {
path[pos ++] = buf[i];
}
DFS(nde[p].left);
DFS(nde[p].right);
path[pos ++] = ')';
}
int main() {
int _, a, b;
scanf("%d", &_);
while(_ --) {
scanf("%d", &N);
sz = N;
init();
for(int i = 0; i < MAXN; i ++) {
Nsc[i].info.clear();
Nsp[i].info.clear();
nde[i].left = nde[i].right = -1;
}
for(int i = 0; i < N - 1; i ++) {
scanf("%d%d", &a, &b);
Nsc[b].info.push_back(a);
Nsp[a].info.push_back(b);
unit(a, b);
}
int flag = 0, root = 0;
if(sz != 1) flag = 1;
for(int i = 1; i <= N; i ++) {
if(Nsc[i].info.size() > 1 && flag == 0) {
flag = 1;
}
}
bool ffg = false;
for(int i = 1; i <= N; i ++) {
if(Nsc[i].info.size() == 0) ffg = true;
}
if(!ffg) flag = 1;
for(int i = 1; i <= N; i ++) {
if(Nsp[i].info.size() > 2 && flag == 0) {
flag = 2;
}
if(Nsc[i].info.size() == 0) root = i;
if(Nsp[i].info.size() >= 1)nde[i].left = Nsp[i].info[0];
if(Nsp[i].info.size() >= 2)nde[i].right = Nsp[i].info[1];
}
//printf("-------\n");
if(flag) {
printf("ERROR%d\n", flag);
continue;
}
fg = false;
DFS1(root);
if(fg && flag == 0) flag = 3;
if(flag) {
printf("ERROR%d\n", flag);
continue;
}
pos = 0;
DFS(root);
path[pos] = '\0';
printf("%s\n", path);
}
return 0;
}
题解
DP处理,第一维为多少步,第二维为第一个人横坐标走了多少步,第三维为第二个人横坐标走了多少步
代码
#include
#include
#include
using namespace std;
const int MAXN = 2e2 + 5;
int a[MAXN << 1][MAXN];
int dp[MAXN << 1][MAXN][MAXN];
const int INF = 0x3f3f3f3f;
int solve(int n) {
int i, j, k;
memset(dp, 0, sizeof(dp));
for(k = 1; k <= 2 * n; k++)
for(i = 1; i <= k && i <= n; i++)
for(j = 1; j <= k && j <= n ; j++) {
int tmp = -INF;
tmp = max(tmp, dp[k - 1][i - 1][j - 1]);
tmp = max(tmp, dp[k - 1][i - 1][j]);
tmp = max(tmp, dp[k - 1][i][j - 1]);
tmp = max(tmp, dp[k - 1][i][j]);
if(i == j) dp[k][i][j] = tmp + a[k - i + 1][i];
else dp[k][i][j] = tmp + a[k - i + 1][i] + a[k - j + 1][j];
}
return dp[2 * n][n][n];
}
int main() {
int n, r, c, v;
while(scanf("%d", &n) != EOF) {
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
scanf("%d", &a[i][j]);
}
}
printf("%d\n", solve(n) + a[1][1] + a[n][n]);
}
return 0;
}