数字三角形 100pts
贪心的想一想,我们从上往下处理每个数,每次向左走,不行再向右走,这样就行(因为右面一定有地方,但我们要尽量留给下一个数);
为什么这样能填满?下面给出证明:
首先,右面和下面不会有空缺(填的方向就是右面和下面);
然后手模一下,我们会发现,其实每个相邻的数是互相制约的,也就是说,当左面的数在向下走时,这个数不能向左走,这样就保证了不会出现一个数上面有空缺的情况;
但是万一左面有空缺,然后这条路被堵住了怎么办?
设当前有三个挨着的数,最上面的是 $ x $,中间的是 $ y $,下面的是 $ z $,下面的拐到中间的数为 $ a $,则出现这种情况,当且仅当满足:
\[ \begin{cases} a + y = x \\ a + y = z \end{cases} \]则:$ x = z $,因为这是个排列,所以不满足;
证毕;
赛时没看见是个排列,导致打出的类广搜正解被我自己手造的不是排列的数据卡掉了,结果换成了100行模拟,好像打出的代码不是排列的也能过?(前提是数据保证有解,但好像没有解也能判?);
点击查看代码
#include <iostream>
#include <cstdio>
using namespace std;
int n;
int a[5005];
int d[5005];
int b[505][505];
pair<int, int> c[505];
bool z(int &x, int &y, int dd) {
if (!b[x][y - 1]) {
y--;
b[x][y] = dd;
return true;
}
return false;
}
bool s(int &x, int &y, int dd) {
if (!b[x - 1][y]) {
x--;
b[x][y] = dd;
return true;
}
return false;
}
bool y(int &x, int &yy, int dd) {
if (!b[x][yy + 1]) {
yy++;
b[x][yy] = dd;
return true;
}
return false;
}
bool x(int &xx, int &y, int dd) {
if (!b[xx + 1][y]) {
xx++;
b[xx][y] = dd;
return true;
}
return false;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
d[i] = a[i];
b[i][i] = a[i];
b[i][i + 1] = 0x3f3f3f3f;
b[i][0] = 0x3f3f3f3f;
b[i - 1][i] = 0x3f3f3f3f;
b[n + 1][i] = 0x3f3f3f3f;
c[i].first = i;
c[i].second = i;
a[i]--;
}
int p = (n + 1) * n / 2;
p++;
for (int i = 1; i <= p; i++) {
for (int j = 1; j <= n; j++) {
if (a[j] == 0) continue;
if (z(c[j].first, c[j].second, d[j])) {
a[j]--;
if (a[j] == 0) continue;
} else if (s(c[j].first, c[j].second, d[j])) {
a[j]--;
if (a[j] == 0) continue;
} else if (y(c[j].first, c[j].second, d[j])) {
a[j]--;
if (a[j] == 0) continue;
} else if (x(c[j].first, c[j].second, d[j])) {
a[j]--;
if (a[j] == 0) continue;
}
}
}
for (int k = 1; k <= n; k++) {
if (a[k] != 0) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (!b[i][j]) {
b[i][j] = d[k];
}
}
}
break;
}
}
for (int i = 1; i <= n; i++) {
if (a[i] != 0) {
cout << -1;
return 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << b[i][j] << ' ';
}
cout << endl;
}
return 0;
}
这种思维题是想到就能打,想不到是真难受;
那一天她离我而去 76pts
暴力删边 + dij 76pts;
考虑正解,其实也是个套路,将与1相连的点按二进制每位的0或1分成两组,前一组与虚点n + 1连边,后一组让1与其连边,这样跑log次dij就能整出来所有的情况(因为相当于每个不同的点对都被分开过),最后用1到n + 1的最短路更新答案即可;
注意初始化;
套路还是要记一下;
点击查看代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
int t;
int n, m;
int dis[10005];
bool vis[10005];
vector<pair<int, int> > v[10005];
vector<pair<int, int> > a, b, c;
int ans;
void dij(int x) {
for (int i = 1; i <= n + 1; i++) {
dis[i] = 0x3f3f3f3f;
vis[i] = false;
}
dis[x] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push({0, x});
while(!q.empty()) {
int xu = q.top().second;
q.pop();
if (vis[xu]) continue;
vis[xu] = true;
for (int i = 0; i < v[xu].size(); i++) {
pair<int, int> u = v[xu][i];
if (dis[u.first] > dis[xu] + u.second) {
dis[u.first] = dis[xu] + u.second;
q.push({dis[u.first], u.first});
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--) {
cin >> n >> m;
c.clear();
for (int i = 1; i <= n; i++) v[i].clear();
ans = 0x3f3f3f3f;
int x, y, z;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
v[x].push_back({y, z});
v[y].push_back({x, z});
}
for (int i = 0; i < v[1].size(); i++) {
c.push_back(v[1][i]);
}
for (int j = 0; j <= log2(n) + 1; j++) {
a.clear();
b.clear();
for (int i = 0; i < c.size(); i++) {
pair<int, int> u = c[i];
if ((u.first >> j) & 1) a.push_back(u);
else b.push_back(u);
}
v[1].clear();
for (int i = 0; i < a.size(); i++) {
v[1].push_back(a[i]);
}
for (int i = 0; i < b.size(); i++) {
v[b[i].first].push_back({n + 1, b[i].second});
}
dij(1);
ans = min(ans, dis[n + 1]);
for (int i = 0; i < b.size(); i++) {
v[b[i].first].pop_back();
}
}
if (ans == 0x3f3f3f3f) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
return 0;
}
To be continued?
标签:赛记,return,19,dd,++,int,push,include,CSP From: https://www.cnblogs.com/PeppaEvenPig/p/18355442