Задача А.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool contains(string &s, char ch) {
for (char x : s) {
if (x == ch) return true;
}
return false;
}
void fixRegister(string &s, bool isBig, int pos) {
if (isBig) s[pos] = toupper(s[pos]);
else s[pos] = tolower(s[pos]);
}
int main() {
string str;
getline(cin, str);
int l = 0, r = str.size() - 1;
string alphabet = "AEIOUY";
while (l < r) {
while (l < r && !contains(alphabet, toupper(str[l]))) l++;
while (l < r && !contains(alphabet, toupper(str[r]))) r--;
if (l < r) {
bool leftIsBig = str[l] == toupper(str[l]);
bool rightIsBig = str[r] == toupper(str[r]);
swap(str[l], str[r]);
fixRegister(str, leftIsBig, l);
fixRegister(str, rightIsBig, r);
}
l++;
r--;
}
cout << str;
return 0;
}
Задача В.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <fstream>
using namespace std;
class Solution {
public:
long long countRangeSum(vector<long long>& nums, long long lower, long long upper) {
int size=nums.size();
if(size==0) return 0;
vector<long> sums(size+1, 0);
for(int i=0; i<size; i++) sums[i+1]=sums[i]+nums[i];
return help(sums, 0, size+1, lower, upper);
}
long long help(vector<long>& sums, int start, int end, long long lower, long long upper){
if(end-start<=1) return 0;
int mid=(start+end)/2;
long long count=help(sums, start, mid, lower, upper)
+ help(sums, mid, end, lower, upper);
long long m=mid, n=mid, t=mid, len=0;
vector<long> cache(end-start, 0);
for(int i=start, s=0; i<mid; i++, s++){
while(m<end && sums[m]-sums[i]<lower) m++;
while(n<end && sums[n]-sums[i]<=upper) n++;
count+=n-m;
while(t<end && sums[t]<sums[i]) cache[s++]=sums[t++];
cache[s]=sums[i];
len=s;
}
for(int i=0; i<=len; i++) sums[start+i]=cache[i];
return count;
}
};
int main() {
Solution s;
int n;
cin >> n;
vector<long long> nums(n, 0);
long long l, r;
cin >> l >> r;
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
cout << s.countRangeSum(nums, l, r) << endl;
}
Задача С.
import sys
def trap(height):
n = len(height)
l, r, water, minHeight = 0, n - 1, 0, 0
while l < r:
while l < r and height[l] <= minHeight:
water += minHeight - height[l]
l += 1
while r > l and height[r] <= minHeight:
water += minHeight - height[r]
r -= 1
minHeight = min(height[l], height[r])
return water
n = int(input())
height = [int(x) for x in input().split()]
print(str(trap(height)))
Задача D.
import sys
def trapRainWater(heightMap):
if not heightMap or not heightMap[0]:
return 0
import heapq
m, n = len(heightMap), len(heightMap[0])
heap = []
visited = [[0] * n for _ in range(m)]
# Push all the block on the border into heap
for i in range(m):
for j in range(n):
if i == 0 or j == 0 or i == m - 1 or j == n - 1:
heapq.heappush(heap, (heightMap[i][j], i, j))
visited[i][j] = 1
result = 0
while heap:
height, i, j = heapq.heappop(heap)
for x, y in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
if 0 <= x < m and 0 <= y < n and not visited[x][y]:
result += max(0, height - heightMap[x][y])
heapq.heappush(heap, (max(heightMap[x][y], height), x, y))
visited[x][y] = 1
return result
n, m = [int(x) for x in input().split()]
matrix = [
[int(x) for x in input().split()]
for _ in range(n)
]
print(str(trapRainWater(matrix)))
Задача Е.
#include <iostream>
using namespace std;
int main()
{
int x;
while (scanf("%d",&x)==1)
{
if (x%8==0) cout<<1; else cout<<0;
}
}