Educational Codeforces Round 145 (Rated for Div. 2) 感想

5 篇文章 0 订阅

A. Garland

3.代码

ll t, n, m, p = 1000000007, k;
ll dp[300005][2];
map<char, int> ci;
string s;

int main() {
cin >> t;
while (t--) {
cin >> s;
ci.clear();
for (int i = 0; i <= s.size(); i++) {
ci[s[i]]++;
}
int ma = 0;
for (auto x : ci) {
ma = max(ma, x.second);
}
if (ma == 1) {
cout << "4\n";
}
if (ma == 2) {
cout << "4\n";
}
if (ma == 3) {
cout << "6\n";
}
if (ma == 4) {
cout << "-1\n";
}
}
return 0;
}


B. Points on Plane

0 2 4 …
1 3 5 …

3.代码

ll t, n, m, p = 1000000007, k;
ll dp[300005][2];
map<char, int> ci;
string s;

int main() {
cin >> t;
while (t--) {
cin >> n;
ll x = sqrt(n);
if (x * x < n) {
cout << x << "\n";
}
else {
cout << x - 1 << "\n";
}
}
return 0;
}


C. Sum on Subarrays

2.个人思路

k的范围应该是(x)(x - 1) / 2 ~ (x)(x - 1) / 2 + x + 1

，需要变化的负数是 -1 - (x+1-k+(x)(x - 1) / 2) * 2;

3.代码

ll t, n, m, p = 1000000007, k;
ll dp[300005][2];
map<char, int> ci;
string s;

int main() {
cin >> t;
while (t--) {
cin >> n >> k;
vector<int> ans;
for (int i = 0; i < n; i++) {
ans.push_back(-1000);
}
int now = 0;
for (int i = 0; i < n; i++) {
now += (i + 1);
if (now <= k) {
ans[i] = 2;
}
if (now < k && now + i + 2 > k) {
ans[i + 1] = -1 - (i + 1 - k + now) * 2;
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}


D. Binary String Sorting

2.个人思路

1. 把出现过的的1都删除掉，代价就是pos[i] * c2，cnt清空。
2. 把这个0删除掉，代价就是dp[i - 1] + c2，cnt保持不变。
3. 把0移动到1之前，代价是dp[i - 1] + cnt[i - 1] * c1，cnt保持不变。

3.代码

ll t, n, m, p = 1000000007, k;
ll c1 = 1e12, c2 = 1e12 + 1;
ll dp[300005], cnt[300005], pos[300005];
string s;

int main() {
cin >> t;
while (t--) {
cin >> s;
for (int i = 0; i < s.size(); i++) {
cnt[i] = 0;
dp[i] = 1e18;
pos[i] = 0;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') pos[i]++;
if (i > 0) pos[i] += pos[i - 1];
}
dp[0] = 0;
for (int i = 0; i < s.size(); i++) {
if (i > 0 && s[i] == '0') {
if (cnt[i - 1] > 0) {
ll xx = pos[i] * c2;
ll yy = dp[i - 1] + cnt[i - 1] * c1;
ll zz = dp[i - 1] + c2;
if (xx <= yy && xx <= zz) {
dp[i] = xx;
cnt[i] = 0;
}
else if(yy < zz && yy < xx) {
dp[i] = yy;
cnt[i] = cnt[i - 1];
}
else {
dp[i] = zz;
cnt[i] = cnt[i - 1];
}
}
else if (cnt[i] == 0) {
dp[i] = dp[i - 1];
}
}
else {
if (i == 0 && s[i] == '1') cnt[i] = 1;
else if (i > 0) {
cnt[i] = cnt[i - 1] + 1;
dp[i] = dp[i - 1];
}
}
}
cout << dp[s.size() - 1] << "\n";
}
return 0;
}


总结

• 0
点赞
• 2
收藏
觉得还不错? 一键收藏
• 0
评论
03-24 318
07-16 162
03-06 700
05-13 766
03-24 682
03-24 610

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。