我是靠谱客的博主 俭朴小土豆,最近开发中收集的这篇文章主要介绍codeforce #582 div3补题A - Chips MovingB - Bad PricesC - Book ReadingD1 - Equalizing by Division (easy version)D2 - Equalizing by Division (hard version),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
A - Chips Moving
数奇数偶数的个数,取最小
//
// main.cpp
// cf_#582_div3_补题
//
// Created by 陈冉飞 on 2019/9/6.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
int a[105];
int main(int argc, const char * argv[]) {
int n,tem,cnt1 = 0,cnt2 = 0;
scanf("%d",&n);
for (int i = 0; i < n; i++) {
scanf("%d",&tem);
if (tem%2 == 0) cnt1++;
else cnt2++;
}
cout<<min(cnt1,cnt2)<<endl;
return 0;
}
B - Bad Prices
反向遍历,只要遍历到的数大于后面的数,就ans++,然后每次刷新从最后到当前的最小的数。
//
// main.cpp
// B
//
// Created by 陈冉飞 on 2019/9/6.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
int T,n,a[150050];
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%d",&n);
int ans = 0;
for (int i = 0; i < n; i++) scanf("%d",&a[i]);
int stdn = a[n-1];
for (int i = n-1; i >= 0; i--) {
if (a[i] >stdn) ans++;
stdn = min(stdn,a[i]);
}
cout<<ans<<endl;
}
return 0;
}
C - Book Reading
直接遍历数据大会超时,然后每次变化相同的数尾数肯定符合规律,必然找到循环节,标记循环节的长度大小最后再遍历比循环节小的那部分。
//
// main.cpp
// C
//
// Created by 陈冉飞 on 2019/9/6.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
typedef long long ll;
int T;
ll n,m;
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%lld%lld",&n,&m);
ll ans = 0;
if (m > n) ans = 0;
else if (m == n) ans = m%10;
else {
//找寻环节
ll tem = m;
while (tem <= n) {
ans += tem%10;
tem += m;
if (tem%10 == m%10) {
tem -= m;
break;
}
}
ans *= (n/tem);
tem *= (n/tem);
while (tem <= n) {
ans += tem%10;
tem += m;
}
}
cout<<ans<<endl;
}
return 0;
}
D1 - Equalizing by Division (easy version)
用二维向量储存数据,数据表示的是所有的数中变化到0,1,2……的个数,然后分别是多少步,所以最后遍历的时候只需要看一下能变化到一个数的个数大于等于k的,然后筛选出变化到一个数的最小三个步数,得到变到这个数的步数,然后最后得到了所有可以得到一个数的步数,选最小的即可。
//
// main.cpp
// D
//
// Created by 陈冉飞 on 2019/9/6.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> vb(200050); //vb用来储存所有的未除到0的前面的所有的次数
int main(int argc, const char * argv[]) {
int n,k,tem;
scanf("%d%d",&n,&k);
for (int i = 0; i < n; i++) {
scanf("%d",&tem);
int cnt = 0;
vb[tem].push_back(0);
while (tem > 0) {
cnt++;
tem /= 2;
vb[tem].push_back(cnt);
}
}
int ans = 0x3f3f3f;
for (int i = 0; i < 200050; i++) {
if (vb[i].size()>=k) {
int tem = 0;
sort(vb[i].begin(), vb[i].end());
for (int j = 0; j < k; j++) {
tem += vb[i][j];
}
ans = min(ans,tem);
}
}
cout<<ans<<endl;
return 0;
}
D2 - Equalizing by Division (hard version)
只是数据量变大
//
// main.cpp
// D
//
// Created by 陈冉飞 on 2019/9/6.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> vb(200050); //vb用来储存所有的未除到0的前面的所有的次数
int main(int argc, const char * argv[]) {
int n,k,tem;
scanf("%d%d",&n,&k);
for (int i = 0; i < n; i++) {
scanf("%d",&tem);
int cnt = 0;
vb[tem].push_back(0);
while (tem > 0) {
cnt++;
tem /= 2;
vb[tem].push_back(cnt);
}
}
int ans = 0x3f3f3f;
for (int i = 0; i < 200050; i++) {
if (vb[i].size()>=k) {
int tem = 0;
sort(vb[i].begin(), vb[i].end());
for (int j = 0; j < k; j++) {
tem += vb[i][j];
}
ans = min(ans,tem);
}
}
cout<<ans<<endl;
return 0;
}
最后
以上就是俭朴小土豆为你收集整理的codeforce #582 div3补题A - Chips MovingB - Bad PricesC - Book ReadingD1 - Equalizing by Division (easy version)D2 - Equalizing by Division (hard version)的全部内容,希望文章能够帮你解决codeforce #582 div3补题A - Chips MovingB - Bad PricesC - Book ReadingD1 - Equalizing by Division (easy version)D2 - Equalizing by Division (hard version)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复