NKOJ12081 士兵

· · 题解

Solution 1

显然差分,一个 [l,r) 权值为 h 的区间等价于可以让 a_l\gets a_l-h,a_r\gets a_r+h。直接暴力枚举每个区间是否选择,复杂度 O(n2^m),期望得分 15\texttt{pts}。

Solution 2

考虑 dp,设状态 f_{i,j,s} 表示对于前 i 个数,满足题目限制的有 j 个,集合 s 表示跨过 i 的区间中选择了哪些。设跨过 i 号点的区间个数为 cnt_i,那么状态总数就是 \sum 2^{cnt_i}i。转移只需要枚举不包含 i-1 却包含了 i 的所有区间的选择情况,由于有 deg_i\le4 的限制,转移的复杂度并不会很大,不妨视为常数。
当然实现的时候可以只转移有值的状态,不难发现随机数据下一大半的状态都没有值,事实上精心构造也无法卡掉这一点。

当然如果只是这样与暴力无异,因为可以被“彩虹”型的数据卡掉。(事实上数据造的比较水,如果直接 dp 应该可以获得不错的分数)
虽然题目的边是有向边,不过我们的 dp 并未使用这一性质,所以不妨视作无向边(这句话的意思是不要像笔者一样有 r 必须要在 l 右侧的刻板印象)。考虑对序列重排,即找到一个排列,使得这个新排列对应的状态数尽可能小。如果要找到状态数最小的排列是困难的,所以直接 shuffle 解决,期望得分 55\texttt{pts}。

Solution 3

特殊性质直接跳过,主要留给乱搞选手发挥。(事实上好像没有人获得这些分?)

事实上,如果 Solution 2 中寻找排列的方式足够优秀,可以在极限数据下把状态数控制到 10^6\sim4\times10^6。这里的依据是,有以下“经典”结论成立:(Acoipp 说的,出锅了找他)

本题则通过这个结论改编而来,因为发现 deg\le3 跑得太快,deg\le5 跑不过,于是设定 deg\le4。

于是我们只需要用尽可能优秀的方式寻找状态数少的排列,而不是使用 shuffle。显然模拟退火非常合适。
期望得分 100\texttt{pts}。

后话

输出方案的原意是 WC2025 T2 也让输出方案,恶心了一众选手(包括本人),于是秉持着有福同享,有难同当的原则,给这个题加了一环。事实上只需要在转移的时候顺便记录前驱状态即可,并没有想过在这个部分区分掉选手,结果有同学退火的部分不够优秀,最后的状态数达到了 10^7,导致直接记录前驱状态会 MLE,最终只能获得 60\texttt{pts}。出题人在此深表歉意。(那些单纯输出方案写挂了的和本来就过不去的不算奥)

Code

仅供模拟退火部分参数调爆炸的选手参考。dp 部分 std 的实现常数很大,什么都没有预处理,建议当个乐子。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int, int>
mt19937 rd(time(0));

const int N = 90, mod = 1e9 + 7;
const LL inf = 1e18;
int n, m, k, l, r, x[N], y[N];
LL hp[N];
struct Edge
{
    int l, r, h;
}edge[N];
vector<int> e[N], s[N];
LL ans;
int p[N], a[N];
struct node
{
    int cnt;
    vector<PII> pre;
};
vector<node> f[N][N];
int to[N]; 

inline void add(node &a, int cnt, PII pre)
{
    (a.cnt += cnt) >= mod && (a.cnt -= mod);
    a.pre.emplace_back(pre);
}

int pos[N], c[N];
LL get()
{
    memset(c, 0, sizeof(c));
    for(int i = 1; i <= n; i++) pos[a[i]] = i;
    for(int i = 1; i <= m; i++)
            c[min(pos[edge[i].l], pos[edge[i].r])]++, c[max(pos[edge[i].l], pos[edge[i].r])]--;
    LL res = 0;
    for(int i = 1, now = 0; i <= n; i++) 
    {
        now += c[i];
        if(now <= 50) res = min(res + (1ll << now) * i, inf);
        else res = inf;
    }
    if(res < ans)
    {
        ans = res;
        memcpy(p, a, sizeof(p));
    }
    return res;
}

void SA()
{
    for(int i = 1; i <= n; i++) a[i] = i;
    shuffle(a + 1, a + n + 1, rd);
    LL now = get();
    for(double t = 1e9; t >= 1e-4; t *= 0.99)
    {
        int x = rd() % n + 1, y = rd() % n + 1;
        swap(a[x], a[y]);
        LL delta = get() - now;
        if(exp(-delta / t) > 1.0 * rd() / UINT_MAX) now += delta;
        else swap(a[x], a[y]);
    }
}

vector<string> plan; 
void dfs(int x, int y, int s1, string pl)
{
    for(int i = 0; i < s[x].size(); i++)
        if(s1 >> i & 1) pl[s[x][i] - 1] = '1';
    if(x == 0)
    {
        plan.push_back(pl), k--;
        return;
    }

    for(PII i : f[x][y][s1].pre)
        if(dfs(x - 1, i.first, i.second, pl), !k) return;
}

int main()
{
    int T;
    cin >> T;
    for(int T1 = 1; T1 <= T; T1++)
    {   
        cin >> n >> m >> k >> l >> r;
        n++;
        for(int i = 1; i < n; i++) scanf("%lld", &hp[i]);
        for(int i = n; i; i--) hp[i] -= hp[i - 1];
        x[1] = x[n] = 1, y[1] = y[n] = 0;
        for(int i = 2; i < n; i++) scanf("%d%d", &x[i], &y[i]);
        for(int i = 1; i <= m; i++) scanf("%d%d%d", &edge[i].l, &edge[i].r, &edge[i].h);

        ans = inf;
        int start = clock();
        while(ans > 1e6 && 1.0 * (clock() - start) / CLOCKS_PER_SEC < 0.08) SA(); 
        cerr << ans << endl;

        for(int i = 1; i <= n; i++) pos[p[i]] = i, s[i].clear(), e[i].clear();
        for(int i = 1; i <= m; i++)
        {
            edge[i].l = pos[edge[i].l], edge[i].r = pos[edge[i].r];
            e[min(edge[i].l, edge[i].r)].push_back(i);
            for(int j = min(edge[i].l, edge[i].r); j < max(edge[i].l, edge[i].r); j++)
                s[j].push_back(i);
        }
        vector<PII> nothing;
        f[0][0].resize(1, {1, nothing});
        for(int i = 1; i <= n; i++)
            for(int j = 0; j <= i; j++)
                f[i][j].clear(), f[i][j].resize(1 << s[i].size(), {0, nothing});

        memset(to, -1, sizeof(to));
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < s[i].size(); j++) to[s[i][j]] = j;
            for(int t = 0; t < i; t++)
                for(int j = 0; j < 1 << s[i - 1].size(); j++)
                {
                    if(!f[i - 1][t][j].cnt) continue;
                    int s1 = 0;
                    LL sum1 = 0;
                    for(int k = 0; k < s[i - 1].size(); k++)
                        if(j >> k & 1)
                        {
                            if(~to[s[i - 1][k]]) s1 |= 1 << to[s[i - 1][k]];
                            else sum1 += edge[s[i - 1][k]].h * (edge[s[i - 1][k]].l == i ? -1 : 1);
                        }
                    for(int k = 0; k < 1 << e[i].size(); k++)
                    {
                        int s2 = 0;
                        LL sum2 = 0;
                        for(int l = 0; l < e[i].size(); l++)
                            if(k >> l & 1) s2 |= 1 << to[e[i][l]], sum2 += edge[e[i][l]].h * (edge[e[i][l]].l == i ? -1 : 1);
                        add(f[i][t + (x[p[i]] <= abs(sum1 + sum2 + hp[p[i]]) && abs(sum1 + sum2 + hp[p[i]]) <= y[p[i]])][s1 | s2], f[i - 1][t][j].cnt, {t, j});
                    }
                }
            for(int j = 0; j < s[i].size(); j++) to[s[i][j]] = -1;
        }

        ans = 0;
        for(int i = l; i <= r; i++) ans += f[n][i][0].cnt;
        if(!ans) puts("-1");
        else 
        {
            cout << ans % mod << endl;
            plan.clear();
            for(int i = l; k && i <= r; i++) dfs(n, i, 0, string(m, '0'));
            cout << plan.size() << endl;
//          for(string i : plan) cout << i << endl;
        }
    }
    return 0;
}