P5019 [NOIP2018 提高组] 铺设道路 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
原理上就是一个差分数组,例如
4 3 2 5 3 5 这个数据
我们在其前面加上一个0作为x值,也就是(0 4 3 2 5 3 5),从4开始输入作为k,若输入的值k大于x,则Sum+=k-x,不管是否大于小于,每次输入后都要将当前k值变为x值
AC:
void solve()
{int ans = 0, k = 0, t;for (int i = 1; i <= n; i++){cin >> t;if (t > k)ans += t - k;k = t;}cout << ans << endl;return;
}
P1434 [SHOI2002] 滑雪 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
虽然是贪心,但是暴力dfs也可以过,从每个点开始搜一次得到每个点的最大长度,再得到总的最长长度
AC:
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
inline int read()
{int k = 0, f = 1; char ch = getchar();while (ch < '0' || ch>'9'){if (ch == '-')f = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){k = k * 10 + ch - '0';ch = getchar();}return k * f;
}
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
int a[110][110];
int b[110][110];
int r, c;
int dfs(int x, int y)
{if (b[x][y])return b[x][y];b[x][y] = 1;int next[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };for (int i = 0; i < 4; i++){int tx = x + next[i][0];int ty = y + next[i][1];if (tx >= 1 && ty >= 1 && tx <= r && ty <= c&&a[tx][ty]<a[x][y]){dfs(tx, ty);b[x][y] = max(b[x][y], b[tx][ty] + 1);}}return b[x][y];
}
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cin >> r >> c;for (int i = 1; i <= r; i++){for (int j = 1; j <= c; j++)cin >> a[i][j];}int ans = 0;for (int i = 1; i <= r; i++){for (int j = 1; j <= c; j++){ans = max(ans, dfs(i, j));}}cout << ans << endl;return 0;
}
P4017 最大食物链计数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
拓扑排序,从最底层开始(即不能吃别人,只能被吃),往前进行搜索,一层一层的进行,每一层搜完后删除这一层,然后把这一层的数值累加到其对应的消费者身上,一直到最高处(使用vector建图,以每个最底层为一个点进行建边,能吃它的就加到它后面)
AC:
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
int n, m;
const int mod = 80112002;
const int N = 5e5 + 10;
int in[N], out[N], num[N];
vector<int>v[N];
queue<int>q;
int main()
{ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= m; i++){int x, y;cin >> x >> y;++in[y], ++out[x];v[x].push_back(y);}for (int i = 1; i <= n; i++){if (!in[i]){num[i] = i;q.push(i);}}while (!q.empty()){int top = q.front();q.pop();int len = v[top].size();for (int i = 0; i < len; i++){int next = v[top][i];--in[next];num[next] = (num[next] + num[top]) % mod;if (in[next] == 0)q.push(v[top][i]);}}int ans = 0;for (int i = 1; i <= n; i++){if (!out[i]){ans = (ans + num[i]) % mod;}}cout << ans << endl;return 0;
}