A.Water Pressure
用 double 直接算即可
ACcode
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
int main(){
double d;
cin >> d;
cout << d/100 << endl;
return 0;
}
B.Election
统计出现次数最多的字符串,暴力统计或者用map统计即可
ACcode
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
map<string, int> v;
int main(){
int n;
cin >> n;
string s, res; int mx = 0;
for(int i = 1; i <= n; i++){
cin >> s;
v[s]++;
if(v[s] > mx){
mx = v[s];
res = s;
}
}
cout << res << endl;
return 0;
}
C.Counting 2
多次查询大于等于 x 的元素的个数,二分即可。
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 5;
int a[maxn];
int main(){
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a+1, a+1+n);
int x;
while(q--){
cin >> x;
int l = 1, r = n, res = 0;
while(l <= r){
int mid = l + r >> 1;
if(a[mid] >= x){
res = n - mid + 1;
r = mid - 1;
}
else l = mid + 1;
}
cout << res << endl;
}
return 0;
}
D.Neighbors
有 n 个人站队(一排),问是否有满足要求的站队方法。
- 每个人最多挨着两个人,即入度小于等于 2
- 不能有出现环( A <-> B, B <-> C, C <-> A),并查集判断即可
ACcode
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
int a[maxn], b[maxn];
int in[maxn], f[maxn];
int find(int x){
if(x == f[x]) return x;
else return f[x] = find(f[x]);
}
bool join(int x, int y){
int fx = find(x);
int fy = find(y);
if(fx == fy) return true;
else{
f[fx] = fy;
return false;
}
}
int main(){
int n, m;
cin >> n >> m;
int u, v;
for(int i = 1; i <= m; i++){
cin >> u >> v;
in[u]++; in[v]++;
a[i] = u; b[i] = v;
}
int res = 1;
for(int i = 1; i <= n; i++) if(in[i] > 2) res = 0;
for(int i = 1; i <= n; i++) f[i] = i;
for(int i = 1; i <= m; i++) if(join(a[i], b[i])) res = 0;
if(res) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
E. Minimal payments
贪心。
常识:买 16 块钱的东西可以付 21 块钱找一张 5 块钱。
对于给定的货币,优先使用面额大的货币。
如何考虑是否要找零,设需要付 X 元,面额 A 大于 X,面额 B 小于 X
- 如果出现 X - (A - X) > B 时,用 A 去支付 X 并找零需要的货币少于等于直接用 B 付
ACcode
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
ll a[105];
int main(){
ll n, x;
cin >> n >> x;
for(int i = 1; i <= n; i++) cin >> a[i];
ll res = 0;
for(int i = n; i >= 1; i--){ // 优先使用大面额
res += x / a[i];
x %= a[i];
// X 比 (a[i] - X) 多出现一个 a[i-1],多理解理解这里
if(x - (a[i] - x) >= a[i-1]) res++, x = a[i] - x;
}
cout << res << endl;
return 0;
}
F.Jealous Two
有 n 种礼物,X 对这 n 种礼物的印象为 A,Y 对这 n 种礼物的印象为 B。
问有多少种送礼组合,可以满足要求。
要求:对于礼物 i 和 j,
A
i
>
=
A
j
并
且
B
i
<
=
B
j
Ai >= Aj 并且 Bi <= Bj
Ai>=Aj并且Bi<=Bj
思路
按照 A 升序排序,对 B 进行离散化。
依次遍历每种礼物,用树状数组(或线段树)统计大于等于当前 B 的礼物的个数。
注意处理一下一模一样的礼物。
ACcode
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
typedef struct Node{
int a;
int b;
} node;
bool cmp(node A, node B){
if(A.a == B.a) return A.b > B.b;
return A.a < B.a;
}
node a[maxn];
int b[maxn];
int t[maxn];
int lowbit(int x){
return x & (-x);
}
ll query(int x){
ll res = 0;
while(x > 0){
res += t[x];
x -= lowbit(x);
}
return res;
}
void add(int x){
while(x < maxn){
t[x]++;
x += lowbit(x);
}
}
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i].a;
for(int i = 1; i <= n; i++) cin >> a[i].b, b[i] = a[i].b;
sort(a+1, a+1+n, cmp); sort(b+1, b+1+n);
for(int i = 1; i <= n; i++) a[i].b = lower_bound(b+1, b+1+n, a[i].b) - b;
ll res = 0, len = 1;
for(int i = 1; i <= n; i++){
res += i - query(a[i].b-1);
add(a[i].b);
if(a[i].a != a[i-1].a || a[i].b != a[i-1].b){
res += len * (len-1) / 2;
len = 1;
}
else len++;
}
res += len * (len-1) / 2;
cout << res << endl;
return 0;
}