Educational Codeforces Round 158 (Rated for Div. 2)

A.找到距离最大,就是最小的汽油存储量。

// Problem: A. Line Trip
// Contest: Codeforces - Educational Codeforces Round 158 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1901/problem/A
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
void solve() {
	int n,x;
	cin>>n>>x;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int ans=0;
	for(int i=2;i<=n;i++){
		ans=max(ans,a[i]-a[i-1]);
	}
	ans=max(ans,2*(abs(a[n]-x)));
	ans=max(ans,a[1]);
	cout<> q;
	while (q--) {
		solve();
	}

	return 0;
}

#############################################################################

B.P5019 [NOIP2018 提高组] 铺设道路,P1969 [NOIP2013 提高组] 积木大赛类似

ans 开long long 

1.模拟题意+贪心,如果左边标记的次数比右边边多就让ans+=左边,右边少,左边标记完,右边肯定就标记完了,ans=-1.

// Problem: B. Chip and Ribbon
// Contest: Codeforces - Educational Codeforces Round 158 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1901/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6+ 9;
ll a[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	ll res=-1;
	for(int i=1;i<=n;i++){
		if(a[i]>a[i-1]){
			res+=a[i]-a[i-1];
		}
	}
	cout<> q;
	while (q--) {
		solve();
	}

	return 0;
}

2.差分+贪心,与1做差分如果大于1就ans+=.

// Problem: B. Chip and Ribbon
// Contest: Codeforces - Educational Codeforces Round 158 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1901/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6+ 9;
ll a[N];
ll diff[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	ll res=0;
	diff[1]=a[1]-1;
	for(int i=2;i<=n;i++){
		diff[i]=a[i]-a[i-1];
	}
	for(int i=1;i<=n;i++){
		if(diff[i]>0){
			res+=diff[i];
		}
	}
	cout<> q;
	while (q--) {
		solve();
	}

	return 0;
}

3.学长思路:观察发现如果c[i] >= c[i + 1]那么每次使用操作2传送回 i 的时候能用操作1把格子i + 1也给走了。也就是说如果有这么一个连续子数组c[i] >= c[i + 1] >= c[i + 2] ...答案就是芯片传送回第i个格子的次数。可以将整个数组划分为若干段这样不上升的子数组。一旦c[i] < c[i + 1]就是新的一段的开始。需要注意的细节是我们在走c[i]的时候能把c[i + 1]也给走了,也就是新的一段需要减去上一段的最后一个元素。由于最开始就在第一个位置,所以需要将最开始的上一段置为1方便统计答案。

#include 
 
#define ll long long
#define db double
typedef std::pair PII;
typedef std::pair> PIII;
typedef std::pair Pll;
typedef std::pair PDD;
using ld = double long;
const long double eps = 1e-9;
int d1[] = {0, 0, 1, -1};
int d2[] = {1, -1, 0, 0};
const int N = 2e5 + 10, M = N << 1;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define ls u << 1
#define rs u << 1 | 1

int n, m, k;
int a[N];

int main(void){
   	std::ios::sync_with_stdio(false);
   	std::cin.tie(0);
   	std::cout.tie(0);
   	
   	int _ = 1;
   	
	std::cin >> _;
	while(_ --) {
		
		std::cin >> n;
	
		for (int i = 1; i <= n; i ++) std::cin >> a[i];
		
		ll ans = 0;
		
		int i = 1, j = 2;
		int last = 1;
		for (; j <= n; j ++) {
			if (a[j] > a[j - 1]) {
				ans += a[i] - last;
				i = j;
				last = a[j - 1];
			}
		}
		ans += a[i] - last;
		
		std::cout << ans << '\n';
		
	}

    return 0;
}

4.维护一个单调栈(*)

#include
#include
typedef long long ll;
using namespace std;
const int N=2e6+9;
ll n, a[N]; 
void solve(){
	cin >> n;
	stack q;
	ll ans = 0;
	for(int i = 0; i < n; ++ i){
		cin >> a[i];
		int pre = -1;
		while(q.size() && q.top() > a[i]){
			if(pre != -1)
				ans += pre - q.top();
			pre = q.top();
			q.pop();
		}
		if(pre != -1) ans += pre - a[i];
		if(q.empty() || q.top() < a[i]){
			q.push(a[i]);
		}
	}
	int pre = -1;
	while(q.size()){
		if(pre != -1)
			ans += pre - q.top();
		pre = q.top();
		q.pop();
	}
	if(pre != 0 && pre != -1) ans += pre;
	cout << ans-1<< '\n';
	return;
}
int main(){
	int q;
	cin>>q;
	while(q--){
		solve();
	}
	return 0;
}

C.将ai改成ceil(ai+x/2)(向下取整),思考怎么样让2个数越来越近,直到相等.我们可以分析数是奇数还是偶数。(向下取整如果是奇数不仅可以让其按2的倍数减少,还可以让奇数+1,让其下降后数慢慢向上移动(与相邻数距离再次变小)(适用于小的数)),(向下取整如果是偶数,+1还是+0都是按2的倍数减少).

因此x取(0,1)就可以解决这个问题。因为如果靠外的数字都相等了,那么中间的数字肯定相等。所以我们只需要考虑最小值和最大值。

分析:4种情况列举(保险)

1.最大值是奇数,最小值是奇数.x=1,x=0都可以.

2.最大值是偶数,最小值是奇数.x=1

3.最大值是奇数,最小值是偶数.x=0

4.最大值是偶数,最小值是偶数.x=0

在每次记录完x后记得要对最大值和最小值进行操作.

// Problem: C. Add, Divide and Floor
// Contest: Codeforces - Educational Codeforces Round 158 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1901/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
int ans[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int cnt=0;
	sort(a+1,a+1+n);
	int minn=a[1];
	int maxn=a[n];
	while(maxn>minn){
		if(maxn-minn==1){
			if(minn%2!=0){
				ans[++cnt]=1;
			}else{
				ans[++cnt]=0;
			}	
			break;
		}else if(maxn-minn>1){
			if(minn%2!=0 && maxn%2==0){
				minn/=2;
				minn++;
				maxn/=2;
				ans[++cnt]=1;
			}else if(minn%2==0 && maxn%2==0){
				minn/=2;
				maxn/=2;
				ans[++cnt]=0;
			}else if(minn%2!=0 && maxn%2!=0){
				minn/=2;
				maxn/=2;
				maxn++;
				minn++;
				ans[++cnt]=1;
			}else{
				minn/=2;
				maxn/=2;
				ans[++cnt]=0;
			}
			
		}
	}
	if(cnt==0){
		cout<<0<<'\n';
		return;
	}
	if(cnt>n){
		cout<> q;
	while (q--) {
		solve();
	}

	return 0;
}

用动态数组实现.(学长)

#include 
 
#define ll long long
#define db double
typedef std::pair PII;
typedef std::pair> PIII;
typedef std::pair Pll;
typedef std::pair PDD;
using ld = double long;
const long double eps = 1e-9;
int d1[] = {0, 0, 1, -1};
int d2[] = {1, -1, 0, 0};
const int N = 2e5 + 10, M = N << 1;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define ls u << 1
#define rs u << 1 | 1

int n, m, k;
int a[N];


int main(void){
   	std::ios::sync_with_stdio(false);
   	std::cin.tie(0);
   	std::cout.tie(0);
   	
   	int _ = 1;
   	
	std::cin >> _;
	while(_ --) {
		std::cin >> n;
	
		for (int i = 1; i <= n; i ++) std::cin >> a[i];
		
		std::sort(a + 1, a + n + 1);
		
		int mn = a[1], mx = a[n];
		
		int ans = 0;
		std::vector tmp;
		while (mn < mx) {
			if (mx - mn == 1) {
				if (mn % 2 == 1) tmp.push_back(1);
				else tmp.push_back(0);
				break;
			}
			if (mn % 2 == 1) {
				mn = (mn + 1) / 2;
				mx = (mx + 1) / 2;
				tmp.push_back(1);
			} else {
				mn = mn / 2;
				mx = mx / 2;
				tmp.push_back(0);
			}
		}
			
		std::cout << tmp.size() << '\n';
		if (tmp.size() <= n && tmp.size()) {
			for (int i = 0; i < tmp.size(); i ++) std::cout << tmp[i] << ' ';
			std::cout << '\n';
		}
		
	}

    return 0;
}

D.枚举第一个被击中的目标(k),做最坏的打算(a[i]刚好为第i个被击中的目标)

假设都是先击中后面的再击中前面的.

分析2种情况,

i>k,最坏是第i个目标会受到x-(i)+1的伤害,要求伤害要大于等于a[i].

i

因此处理前缀和(prefix)和后缀和(suffix)的最大值,再进行比较取min.

ans开long long .
 

// Problem: D. Yet Another Monster Fight
// Contest: Codeforces - Educational Codeforces Round 158 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1901/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 3e5 + 9;
ll a[N];
ll prefix[N],suffix[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	prefix[0]=suffix[n+1]=0;
	for(int i=1;i<=n;i++){
		prefix[i]=max(prefix[i-1],a[i]-i+n);
	}
	for(int i=n;i>=1;i--){
		suffix[i]=max(suffix[i+1],a[i]+i-1);	
	}
	ll ans=1e18;
	for(int i=1;i<=n;i++){
		//1.从前面开始攻击2.从后面开始攻击
		ans=min(ans,max({prefix[i-1],suffix[i+1],a[i]}));
	}
	cout<

你可能感兴趣的:(算法)