博主没有学习过数据结构,纯纯是陪跑的,来试试强度的。前两题都是签到题,第三题是思维题,第四题我用的multiset写的,这题考点好像叫平衡二叉搜索树,我不是很懂,也是蒙出来了。最后两题大概知道考啥,但是完全不会写,第五题应该考的是树状数组、线段树,第六题是考hash。
参考题解:
#include
#define endl '\n'
using namespace std;
using ll = long long;
//using PII = pair;
const int N = 1e4+5;
void solve(){
int n;cin >> n;
int cnt = 0;
while(n!=0){
if(a%2==1) cnt++;
a/=2;
}
cout << cnt << endl;
}
int main(){
//1. 美丽的2024【算法赛】
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
参考题解:
#include
#define endl '\n'
using namespace std;
using ll = long long;
//using PII = pair;
const int N = 1e2+5;
string s[N];
void solve(){
int n;cin >> n;
for(int i = 1;i<=n;i++) cin >> s[i];
int ans = 0;
for(int i = 1;i<=n;i++){
if(s[i]=="yuanxing") ans++;
else if(s[i]=="zhengfangxing") ans+=2;
else if(s[i]=="changfangxing") ans+=3;
else if(s[i]=="sanjiaoxing") ans+=4;
else if(s[i]=="tuoyuanxing") ans+=5;
else if(s[i]=="liubianxing") ans+=6;
}
cout << ans << endl;
}
int main(){
//2. 自助餐【算法赛】
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
参考题解:
#include
#define endl '\n'
using namespace std;
using ll = long long;
//using PII = pair;
const int N = 1e4+5;
void solve(){
int n;cin >> n;
cout << (n&1?'A':'B') << endl;
}
int main(){
//
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
参考题解:
#include
#define endl '\n'
using namespace std;
using ll = long long;
//using PII = pair;
const int N = 1e5+5;
int age[MAX];
string check(int l, int r) {
multiset ages;
for(int i=l; i<=r; i++) {
auto it = ages.lower_bound(age[i]-365);
if(it != ages.end() && *it <= age[i]+365) return "YES";
ages.insert(age[i]);
}
return "NO";
}
void solve(){
int n,q;cin >> n >> q;
for(int i=1; i<=n; i++) cin >> age[i];
while(q--){
int l, r;
cin >> l >> r;
cout << check(l, r) << "\n";
}
}
int main(){
//4. 乘飞机【算法赛】
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
参考题解1:(大佬写的,感谢带专魂大佬提供的题解)
#include
using i64 = long long;
using namespace std;
template struct Fenwick {
int n;
vector tr;
Fenwick(int n) : n(n), tr(n + 1, 0) {}
void insert(int x, const T v) {
for (; x <= n; x += x & -x) {
tr[x] += v;
}
}
T query(int x) {
T res = 0;
for (; x; x -= x & -x) {
res += tr[x];
}
return res;
}
T query(int l, int r) {
return query(r) - query(l - 1);
}
};
void solve(){
int n;
cin >> n;
vector a(n + 1), b(n + 1), pos(n + 1);
for (int i = 1; i <= n; i ++ ){
cin >> a[i];
}
for (int i = 1; i <= n; i ++ ){
cin >> b[i];
pos[b[i]] = i;
}
Fenwick bit1(n), bit2(n);
i64 ans = 0;
for (int i = 1; i <= n; i ++ ){
ans += 1ll * i * bit2.query(pos[a[i]]) - bit1.query(pos[a[i]]);
bit2.insert(pos[a[i]], 1);
bit1.insert(pos[a[i]], i);
}
cout << ans << "\n";
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int _ = 1;
//cin >> _;
while (_ -- ){
solve();
}
return 0;
}
参考题解2:(另一个大佬写的,感谢Sadbo1大佬提供的题解)
#include
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define int long long
#define ull unsigned long long
#define lowbit(i) ((i)&(-i))
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
#define rep(i, a, b) for(int i=a;i<=b;i++)
#define per(i, a, b) for(int i=a;i>=b;i--)
typedef pair PII;
const int mod = 1e9 + 7;
const int inf = 4e18;
const int N = 2e5 + 200;
template
class BIT {
public:
std::vector c;
int sz;
BIT(int n) : c(n + 1) { sz = n; }
void resize(int n) {
c.resize(n + 1);
sz = n;
}
void add(int pos, T k) {
while (pos <= sz) {
c[pos] += k;
pos += lowbit(pos);
}
}
void add(int l, int r, int k) {
BIT::add(l, k);
BIT::add(r + 1, -k);
}
T query(int i) {
T res = 0;
while (i > 0) {
res += c[i];
i -= lowbit(i);
}
return res;
}
};
int a[N], b[N], pos[N];
signed main() {
IOS
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
BIT bit1(n), bit2(n);
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += bit1.query(pos[b[i]]) * pos[b[i]] - bit2.query(pos[b[i]]);
bit1.add(pos[b[i]], 1);
bit2.add(pos[b[i]], pos[b[i]]);
}
cout << ans << endl;
return 0;
}
题解还没出来,再等等