Codeforces Round #683 (Div. 2, by Meet IT)

A Add Candies

输出1就行

int main() {
	IOS;
	for (cin >> _; _; --_) {
		cin >> n;
        cout << n << '\n';
        rep (i, 1, n) cout << i << ' ';
        cout << '\n';
	}
	return 0;
}

B Numbers Box

仔细观察可以发现, 两个相邻的减号可以消掉

我们可以将一个减号, 通过操作传递到任何一个位置

故, 最后最多剩下一个减号(负数), 贪心选绝对值最小的就行

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m; ll ans = 0;
        vector<int> x, y;
        rep(i, 1, n)
            rep(j, 1, m) {
            cin >> a[i][j]; ans += abs(a[i][j]);
            if (a[i][j] < 0) x.pb(a[i][j]);
            else y.pb(a[i][j]);
        }
        sort(all(x)); sort(all(y));
        if (x.size() & 1) {
            if (!y.empty() && y[0] < abs(x.back())) ans -= y[0] << 1;
            else ans += x.back() << 1;
        }
        cout << ans << '\n';
    }
    return 0;
}

C Numbers Box

贪心, 先选最大的

PLL a[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        ll w, m; cin >> n >> w; m = w;
        rep (i ,1, n) cin >> a[i].fi, a[i].se = i;
        sort(a + 1, a + 1 + n, greater<PLL>());
        VI ans;
        rep (i, 1, n) if (w >= a[i].fi) ans.pb(a[i].se), w -= a[i].fi;
        sort(all(ans));
        if ((w << 1) > m) { cout << "-1\n"; continue; }
        cout << ans.size() << '\n';
        for (auto &i : ans) cout << i << ' ';
        cout << '\n';
    }
    return 0;
}

D Catching Cheaters

这道题一共考察了两个dp 模型

第一个是最长公共子序列, 一个是连续区间和最大

最长公共子序列就不说了, 题目都告诉你了

我们观察以下式子, 4 * lcs - lenx- leny = (2 * lcs - lenx) + (2 * lcs - leny)

(2 * lcs - lenx) 就是 lcs长度 减去不是 lcs的长度, 所以 f[i][j] 的转移公式就成了

if (a[i] == b[i]) f[i][j] = max(f[i - 1][j - 1], 0) + 2; //负数直接扔, 连续区间和一样
else f[i][j] = max(f[i][j - 1], f[i- 1][j]) - 1;

int main() {
    IOS; cin >> n >> m >> a + 1 >> b + 1;
    rep (i, 1, n) {
        rep (j, 1, m) {
            f[i][j] = max(f[i - 1][j], f[i][j - 1]) - 1;
            if (a[i] == b[j]) umax(f[i][j], max(f[i - 1][j - 1], 0ll) + 2);
            umax(ans, f[i][j]);
        }
    }
    cout << ans;
    return 0;
}

E Xor Tree

建 trie树, 只能留一颗最大的树和一个分支

struct Trie {
	static const int N = 31 * 2e5;

	int tr[N][2], tot = 1;

	void insert(int x) {
		int p = 1;
		per (i, 31, 0) {
			int ch = x >> i & 1;
			if (!tr[p][ch]) tr[p][ch] = ++tot;
			p = tr[p][ch];
		}
	}

	int dfs(int p) {
		if (!p) return 0;
		if (!tr[p][0] && !tr[p][1]) return 1;
		return max(dfs(tr[p][0]) + (tr[p][1] > 0), dfs(tr[p][1]) + (tr[p][0] > 0));
	}
} T;

int main() {
    IOS: cin >> n;
	rep (i, 1, n) cin >> m, T.insert(m);
	cout << n - T.dfs(1);
    return 0;
}
posted @ 2020-11-16 17:36  洛绫璃  阅读(331)  评论(6编辑  收藏  举报