传送门
A - 小红的字符移动
分析
- 纯模拟题,交换一下位置直接输出即可
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))
constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
char ch[10];
cin >> ch;
std::swap(ch[0], ch[1]);
cout << ch;
return 0;
}
B - 小红的数轴移动
分析
- 这题同样是个模拟题,当回到原点时需要break
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))
constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
i64 n, x;
cin >> n >> x;
std::vector<i64> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
i64 ans = 0;
for(int i = 0; i < n; i++) {
if(x > 0) {
ans += a[i];
x -= a[i];
} else if(x < 0) {
ans += a[i];
x += a[i];
} else {
break;
}
}
cout << ans;
return 0;
}
C - 小红的圆移动
分析
- 我们记录刚开始就包含原点的圆让它不包含原点时需要的代价,同时记录开始不包含原点的圆让它包含原点时需要的代价,当包含原点的圆的个数 x > k x >k x>k时,只需要移动一开始就包含圆的最小代价的前 x − k x - k x−k个即可,当包含原点的圆的个数 x < k x < k x<k时,只需要移动一开始不包含圆的最小代价的前 k − x k - x k−x个即可,记得输出保留 6 位小数 6位小数 6位小数
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))
constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9, PI = acos(-1);
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cout << std::fixed << std::setprecision(6);
int n, k;
cin >> n >> k;
int c = 0;
std::vector<double> a1, a2;
for(int i = 0; i < n; i++) {
i64 x, y, r;
cin >> x >> y >> r;
if(x * x + y * y < r * r) {
a1.push_back(PI * r * r * (r - std::sqrt(x * x + y * y)));
c++;
} else {
a2.push_back(PI * r * r * (std::sqrt(x * x + y * y) - r));
}
}
std::sort(all(a1));
std::sort(all(a2));
if(c == k) {
cout << 0 << "\n";
} else if(c > k) {
double ans = 0;
for(int i = 0; i < c - k; i++) {
ans += a1[i];
}
cout << ans << "\n";
} else {
double ans = 0;
for(int i = 0; i < k - c; i++) {
ans += a2[i];
}
cout << ans << "\n";
}
return 0;
}
D - 小红的树上移动
分析
- 首先一开始所有结点都是白色,对于当前位置 u u u,设到当前节点前面的步骤所得到的总概率为 p p p,那么对于节点 u u u 的孩子节点,设孩子有 x x x 个,每个节点都有 1 / x 1/x 1/x 的概率被染色,那么加上前面的总共就是 p / x p / x p/x 的概率,需要特判一下没有孩子的情况,此时就是 p p p
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))
constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p) {
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = Md7;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = Md7;
using Z = MInt<P>;
Z ans = 0;
std::vector<int> adj[100010];
void dfs(int u, int fa, Z p){
ans += p;
int cnt = 0;
for(auto& v : adj[u]) {
if(v == fa) {
continue;
}
cnt++;
}
Z nxt = p / Z(cnt == 0 ? 1 : cnt);
for(auto& v : adj[u]) {
if(v == fa) {
continue;
}
dfs(v, u, nxt);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1, 1);
cout << ans;
return 0;
}