codeforces 1213A. Chips Moving,B. Bad Prices

A. Chips Moving 

题目链接:codeforces 1213A

题意:

   给n个数,向左右平移两个单位免费,向左右平移一个单位花费1 元,问最少花费多少使最终在一个点上

题解:

   很好想,奇数往奇数上平移免费,向偶数平移花费1,所以判断奇数和偶数的大小,平移小的

c++

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, even = 0, odd = 0;
	cin >> n;
	for(int i = 1; i <= n; i++){
		int t;
		cin >> t;
		if(t % 2 == 0){
			even++;
		}
		else{
			odd++;
		}
	}
	cout << min(even, odd);
	return 0;
} 

php

<?php
	$cin = fopen("php://stdin", "r");
	$n = intval(fgets($cin));
	$s = fgets($cin);
	$a = explode(" ", $s);
	$even = 0;
	$odd = 0;
	for($i = 0; $i < count($a); $i++){
		if(intval($a[$i]) % 2 == 0){
			$even++;
		}
		else{
			$odd++;
		}
	}
	print_r(min($even, $odd)."\n");
?>

B. Bad Prices

题目链接:codeforces 1213B

题意:

   给个数组,如果后面有个比当前数字小的数,那么它是 bad Price ,求有多少个badPrice

题解:

    单调队列求解就好,如果有比top小的, pop出来,pop数的个数就是答案

#include <bits/stdc++.h>
using namespace std;
stack<int> s;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, ans = 0;
		while (!s.empty()){
			s.pop();
		}
		cin >> n;
		for(int i = 1; i <= n; i++){
			int x;
			cin >> x;
			if(s.empty()){
				s.push(x);
			}
			else{
				while(!s.empty() && s.top() > x){
					s.pop();
					ans++;
				}
				s.push(x);
			}
		}
		cout << ans << endl;
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值