Kanon 40pts
簽到題,但是不會,所以打了暴力;
正解時考慮相鄰兩個雪球,只有兩種情況:它們的覆蓋區間有交集或無交集,那麼如果我們找出了無交集的最後一天,我們就很容易判斷剩下的一堆雪該被誰拿走,於是我們二分找出這一天即可;賽時確實想不到二分
時間複雜度:$ \Theta(n \log n) $;
點選檢視程式碼
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n, q;
long long a[500005];
long long w[500005];
long long sum[500005];
long long an[500005];
long long ma[500005], mi[500005];
namespace BIT{
long long tr[2][5000005];
inline int lowbit(int x) {
return x & (-x);
}
void add(int pos, long long d) {
for (int i = pos; i <= q; i += lowbit(i)) {
tr[0][i] = max(tr[0][i], d);
tr[1][i] = min(tr[1][i], d);
}
}
long long ask(int s, int pos) {
if (s == 0) {
long long ans = -0x3f3f3f3f3f3f3f3f;
for (int i = pos; i; i -= lowbit(i)) {
ans = max(ans, tr[0][i]);
}
return ans;
} else {
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int i = pos; i; i -= lowbit(i)) {
ans = min(ans, tr[1][i]);
}
return ans;
}
}
}
using namespace BIT;
bool ck(int x, long long s) {
long long mma = ma[x];
long long mmi = mi[x];
if (mmi > 0) return mma <= s;
else return mma - mmi <= s;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= q; i++) {
cin >> w[i];
}
for (int i = 1; i <= n; i++) {
tr[0][i] = -0x3f3f3f3f3f3f3f3f;
tr[1][i] = 0x3f3f3f3f3f3f3f3f;
}
long long mma = 0;
long long mmi = 0;
for (int i = 1; i <= q; i++) {
sum[i] = sum[i - 1] + w[i];
mma = max(mma, sum[i]);
mmi = min(mmi, sum[i]);
add(i, sum[i]);
ma[i] = max(0ll, ask(0, i));
mi[i] = min(0ll, ask(1, i));
}
an[1] += (-mmi);
an[n] += mma;
for (int i = 1; i <= n - 1; i++) {
int l = 1;
int r = q;
int ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if (ck(mid, a[i + 1] - a[i])) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
mma = ma[ans];
mmi = mi[ans];
ans++;
an[i + 1] += (-mmi);
an[i] += mma;
if (ans > q) continue;
if (w[ans] < 0) {
an[i + 1] -= (-mmi);
an[i + 1] += (a[i + 1] - a[i] - mma);
} else {
an[i] -= mma;
an[i] += (a[i + 1] - a[i] + mmi);
}
}
for (int i = 1; i <= n; i++) {
cout << an[i] << '\n';
}
return 0;
}
黎明與螢火 0pts
簽到題爆零。。。
賽時打的是正解,但沒有判掉已經確定的答案導致錯誤;
考慮從下往上找(因為從上往下可能會使原來根節點和數個子節點都是偶數,結果刪了根節點後數個子節點都是奇數導致不能再選,而從下往上可以反過來甚至避免這個問題,總的來說就是儘量選一個點,使其影響的點較少),那麼我們可以採用類似拓撲排序的思路,開一個優先佇列,過載運算子使其將深度大的且是偶數的點放在上面,每次取出隊頭是判斷一下現在的奇偶以及被計算進答案的情況,然後將其符合條件且未被計算進答案的點放進去,完事以後判斷一下即可;
時間複雜度:每個點至多會進隊其度數次(這也是正確性的保證),但是總的遍歷次數是度數次的,乘上優先佇列的複雜度,那麼總的時間複雜度為 $ \Theta(n \log n) $;
點選檢視程式碼
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n;
vector<int> v[500005];
vector<int> ans;
int d[500005], dep[500005], dd[500005];
bool vis[500005];
struct sss{
int x;
bool operator <(const sss &A) const {
return dep[x] < dep[A.x];
}
bool operator >(const sss &A) const {
return dep[x] < dep[A.x];
}
};
priority_queue<sss> q;
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
for (int i = 0; i < v[x].size(); i++) {
int u = v[x][i];
if (u == fa) continue;
dfs(u, x);
}
}
bool solve() {
while(!q.empty()) {
while(!q.empty() && (d[q.top().x] % 2 != 0 || vis[q.top().x])) q.pop();
if (q.empty()) break;
sss tt = q.top();
q.pop();
int t = tt.x;
vis[t] = true;
d[t] = 0;
ans.push_back(t);
for (int i = 0; i < v[t].size(); i++) {
int u = v[t][i];
if (vis[u]) continue;
d[u]--;
if (d[u] % 2 == 0) {
q.push(sss{u});
}
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x == 0) continue;
v[i].push_back(x);
v[x].push_back(i);
d[x]++;
d[i]++;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
if (d[i] % 2 == 0) {
q.push(sss{i});
}
}
if (solve()) {
cout << "YES" << '\n';
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << '\n';
}
} else {
cout << "NO";
}
return 0;
}
Darling Dance 10pts
賽時沒看這道題,輸出了個0就走了,拿了10pts
這個題把最短路DAG建出來就行,然後從1開始對其BFS(DFS也行),選k條邊即可;
對於最短路DAG,只需在最短路成功更新時記錄一下從哪裡更新的即可(即其前驅節點);
注意k=0的情況;
點選檢視程式碼
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int n, m, k;
struct sss{
int t, ne, w;
}e[2000005];
int h[2000005], cnt;
void add(int u, int v, int ww) {
e[++cnt].t = v;
e[cnt].ne = h[u];
h[u] = cnt;
e[cnt].w = ww;
}
int dis[500005];
bool vis[500005];
int fr[500005], id[500005];
vector<int> v[500005];
int w(int x, int y) {
if (x % y == 0) return x / y;
else return x / y + 1;
}
void dij(int x) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
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 = h[xu]; i; i = e[i].ne) {
int u = e[i].t;
if (dis[u] > dis[xu] + e[i].w) {
dis[u] = dis[xu] + e[i].w;
fr[u] = xu;
id[u] = w(i, 2);
q.push({dis[u], u});
}
}
}
}
queue<int> p;
int ans[500005], o;
void bfs() {
p.push(1);
while(!p.empty()) {
int t = p.front();
p.pop();
for (int i = 0; i < v[t].size(); i++) {
int u = v[t][i];
if (o == k) return;
p.push(u);
ans[++o] = id[u];
}
}
}
int main() {
cin >> n >> m >> k;
int x, y, w;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> w;
add(x, y, w);
add(y, x, w);
}
dij(1);
for (int i = 1; i <= n; i++) {
v[fr[i]].push_back(i);
}
bfs();
cout << o << '\n';
for (int i = 1; i <= o; i++) {
cout << ans[i] << ' ';
}
return 0;
}
最近比賽打的真是越來越CD了。。。