2024牛客寒假训练营1总结

G题不开long long的后果,即使有思路也没用。(给我气的)

2024牛客寒假训练营1总结_第1张图片 

E题,不看数据范围的后果,不能一题名取题啊。

 

using ll = long long;
void solve() {
	int n, m;
	std::cin >> n >> m;
	std::vectora(n);
	for (int i = 0; i < n; i++) {
		std::cin >> a[i];
	}
	std::vectoru(m), v(m);
	for (int i = 0; i < m; i++) {
		std::cin >> u[i] >> v[i];
		u[i]--, v[i]--;
	}
	int ans = n;
	auto dfs = [&](auto self, int i)->void {
		if (i == m) {
			int res = 1;
			for (int j = 0; j < n; j++) {
				res += (a[j] > a[0]);
			}
			ans = std::min(ans, res);
			return;
		}
		for (auto [x, y] : { std::make_pair(3,0),{0,3},{1,1} }) {
			a[u[i]] += x;
			a[v[i]] += y;
			self(self, i + 1);
			a[u[i]] -= x;
			a[v[i]] -= y;
		}
	};
	dfs(dfs, 0);
	std::cout << ans << '\n';
}
int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
	int t;
	std::cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

 jiangly的代码,当中值得学习的是这个dfs,其中这个查找排名这个代码是我没想到的

无序序列中O(n)查找该数的排名。

for (int j = 0; j < n; j++) {
	res += (a[j] > a[0]);
}

看别人代码感觉很简单,自己写就写不出来(*/ω\*) 。

C题也是……ε=(´ο`*)))唉,o(╥﹏╥)o,鉴定为语文能力不好

Sc-Smin<=M,Sc=a1+……ak+……an+(n-k+1)*tc,Smin=a1+……ak+……an,两个一减不就是,p*tc=M吗,把tc除过去,如果p超过了n那就取n嘛,不然就取p嘛。真的服了自己这个脑子怎么就没想到这样写。稍微推导一下就出来了,虽然但是,这个读题能力怎么提升啊……

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, Q, tc;
    std::cin >> n >> Q >> tc;
    
    std::vector t(n);
    std::vector s(n + 1);
    for (int i = 0; i < n; i++) {
        std::cin >> t[i];
    }
    std::sort(t.begin(), t.end());
    for (int i = 0; i < n; i++) {
        s[i + 1] = s[i] + t[i];
    }
    
    while (Q--) {
        i64 M;
        std::cin >> M;
        
        i64 c = std::min(1LL * n, M / tc);
        i64 ans = s[n - c] + tc;
        std::cout << ans << "\n";
    }
    
    return 0;
}

剩下的明天再更吧。不,睡醒了在更。

-----------------------------------------------------2024/2/3/1:10----------------------------------------------------------

你可能感兴趣的:(算法,题目讲解)