Artful Paintings
[Link](Problem - A - Codeforces)
题意
输入
n
,
m
1
,
m
2
n,m_1,m_2
n,m1?,m2?表示有
n
n
n个立方体和
m
1
m_1
m1?个第一种限制:
[
l
,
r
]
[l,r]
[l,r]区间染色的立方体的数目应该不少于
k
k
k个,
m
2
m_2
m2?个第二种限制:
[
l
,
r
]
[l,r]
[l,r]区间外染色的立方体的数目应该不少于
k
k
k个,问你满足情况的最小染色立方体。
题解
假设染k个立方体成立那么染k+1个也一定成立,发现答案具有二段性,所以可以二分答案,找到最小的染色。进而发现这道题是要求满足一些列的等式是否存在可行解是一个差分约束问题,考虑染色数的前缀和
S
i
S_i
Si?,那么
S
i
S_i
Si?一定要满足
S
l
?
1
?
S
r
≤
?
k
S_{l-1}-S_r\le -k
Sl?1??Sr?≤?k
S
r
?
S
l
?
1
≤
m
i
d
?
k
S_r-S_{l-1}\le mid-k
Sr??Sl?1?≤mid?k
S
i
?
S
i
?
1
≤
1
S_i-S_{i-1}\le 1
Si??Si?1?≤1
S
i
?
1
?
S
i
≤
0
S_{i-1} -S_i\le 0
Si?1??Si?≤0
S
n
?
S
0
≤
m
i
d
S_n-S_0\le mid
Sn??S0?≤mid
S
0
?
S
n
≤
?
m
i
d
S_0-S_n\le -mid
S0??Sn?≤?mid
按照后面向前面连边权为右边数的边,然后跑spfa判断是否有负环就可,直接做spfa会T,考虑优化,当有一点的dist为负,那么一定存在负环直接返回即可,因为存在一条
i
?
>
i
?
1
i->i-1
i?>i?1边权为0的边,因此从源点出发再回源点一定是负环。
这样其实是求最大值,因此可以用最短路,
也可以列这样的式子
S
r
?
S
l
?
1
≥
k
S_r-S_{l-1}\ge k
Sr??Sl?1?≥k
S
l
?
1
?
S
r
≥
k
?
m
i
d
S_{l-1}-S_r\ge k-mid
Sl?1??Sr?≥k?mid
S
i
?
S
i
?
1
≥
0
S_i-S_{i-1}\ge 0
Si??Si?1?≥0
S
i
?
1
?
S
i
≥
?
1
S_{i-1} -S_i\ge -1
Si?1??Si?≥?1
S
n
?
S
0
≥
m
i
d
S_n-S_0\ge mid
Sn??S0?≥mid
S
0
?
S
n
≥
?
m
i
d
S_0-S_n\ge -mid
S0??Sn?≥?mid
这样是求最小值,因此可以用最长路,
但是发现这个没办法用上上面那样优化,因为它有的是
i
?
1
?
>
i
i-1->i
i?1?>i边权为0的路,我们可以用一个栈来跑spfa而不是用队列,这样的话就可以快速的找到一个正环了。
Code-1
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m1, m2, k;
int a[N];
int dist[N], cnt[N];
bool st[N];
queue<int> q;
struct Node {
int l, r, k;
}e1[N], e2[N];
bool spfa() {
while(q.size()) q.pop();
for (int i = 0; i <= n; i ++ ) dist[i] = INF, cnt[i] = 0, st[i] = false;
dist[0] = 0, st[0] = true, cnt[0] = 1;
q.push(0);
while (q.size()) {
int t = q.front(); q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i] ) {
int j = e[i];
if (dist[j] > dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
if (dist[j] < 0) return false;
if (!st[j]) {
st[j] = true;
q.push(j);
if (++ cnt[j] > n) return false;
}
}
}
}
return true;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T -- ) {
cin >> n >> m1 >> m2;
for (int i = 1; i <= m1; i ++ ) {
int l, r, k; cin >> l >> r >> k;
e1[i] = {l, r, k};
}
for (int i = 1; i <= m2; i ++ ) {
int l, r, k; cin >> l >> r >> k;
e2[i] = {l, r, k};
}
int l = 0, r = n, res = 0;
while (l < r) {
int mid = l + r >> 1;
for (int i = 0; i <= n; i ++ ) h[i] = -1;
idx = 0;
for (int i = 1; i <= n; i ++) add(i - 1, i, 1), add(i, i - 1, 0);
add(0, n, mid), add(n, 0, -mid);
for (int i = 1; i <= m1; i ++ ) add(e1[i].r, e1[i].l - 1, -e1[i].k);
for (int i = 1; i <= m2; i ++ ) add(e2[i].l - 1, e2[i].r, mid - e2[i].k);
if (spfa()) r = mid;
else l = mid + 1;
res = max(l, r);
}
cout << res << endl;
}
return 0;
}
Code-2
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m1, m2, k;
int a[N];
int dist[N], cnt[N];
bool st[N];
stack<int> q;
struct Node {
int l, r, k;
}e1[N], e2[N];
bool spfa() {
while(q.size()) q.pop();
for (int i = 0; i <= n; i ++ ) dist[i] = -INF, cnt[i] = 0, st[i] = false;
dist[0] = 0, st[0] = true, cnt[0] = 1;
q.push(0);
while (q.size()) {
int t = q.top(); q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i] ) {
int j = e[i];
if (dist[j] < dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[t] >= n + 1) return false;
if (!st[j]) {
st[j] = true;
q.push(j);
}
}
}
}
return true;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T -- ) {
cin >> n >> m1 >> m2;
for (int i = 1; i <= m1; i ++ ) {
int l, r, k; cin >> l >> r >> k;
e1[i] = {l, r, k};
}
for (int i = 1; i <= m2; i ++ ) {
int l, r, k; cin >> l >> r >> k;
e2[i] = {l, r, k};
}
int l = 0, r = n, res = 0;
while (l < r) {
int mid = l + r >> 1;
for (int i = 0; i <= n; i ++ ) h[i] = -1;
idx = 0;
for (int i = 1; i <= n; i ++) add(i - 1, i, 0), add(i, i - 1, -1);
add(0, n, mid), add(n, 0, -mid);
for (int i = 1; i <= m1; i ++ ) add(e1[i].l - 1, e1[i].r, e1[i].k);
for (int i = 1; i <= m2; i ++ ) add(e2[i].r, e2[i].l - 1, e2[i].k - mid);
if (spfa()) r = mid;
else l = mid + 1;
res = max(l, r);
}
cout << res << endl;
}
return 0;
}
|