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;
}