笔试——Day43

文章目录

第一题

题目

kotori和抽卡(二)

思路

数学: 二项分布C(n, m) * p ^ n * (1 - p) ^ m

代码

cpp 复制代码
#include<iostream>
using namespace std;

int n, m;

int main()
{
    cin >> n >> m;
    double res = 1.0;
    for(int i = n; i >= n - m + 1; i--) res *= i;
    for(int i = m; i >= 1; i--) res /= i;
    for(int i = 0; i < m; i++) res *= 0.8;
    for(int i = 0; i < n - m; i ++) res *= 0.2;
    
    printf("%.4f\n", res);
    
    return 0;
}

第二题

题目

ruby和薯条

思路 1

  • 排序数组:首先对数组进行排序
  • 遍历每个元素作为基准:对于每个元素a[i],寻找所有满足条件的a[j]
  • 使用二分查找确定范围
    • 找到第一个 ≥ a[i] + l 的位置(左边界)
    • 找到第一个 > a[i] + r 的位置(右边界)
  • 统计对数:两个边界之间的元素个数就是满足条件的对数

代码1

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 200010;

int n, l, r;
int a[N];

int main() {
    cin >> n >> l >> r;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    
    long long res = 0;
    for (int i = 0; i < n - 1; i++) 
    {
        int left_bound = a[i] + l;
        int right_bound = a[i] + r;
        // 找到第一个大于等于left_bound的位置
        int j = lower_bound(a + i + 1, a + n, left_bound) - a;
        // 找到第一个大于right_bound的位置
        int k = upper_bound(a + i + 1, a + n, right_bound) - a;
        res += (k - j);
    }
    
    cout << res << endl;
    
    return 0;
}

思路2

  • [l, r]之间⼀共有多少对等价于[0,r] - [0, l -1]有多少对
  • 利用滑动窗口解决[0, x]有多少对;

代码2

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 200010;

int n, l, r;
int a[N];

// 找出差值在 [0, x] 之间⼀共有多少对
long long solve(int x)
{
    long long res = 0;
    int left = 0, right = 0;
    while(right < n)
    {
        while((a[right] - a[left]) > x) left++;
        res += right - left;
        right++;
    }
    return res;
}

int main() 
{
    cin >> n >> l >> r;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    
    cout << solve(r) - solve(l - 1) << endl;
    
    return 0;
}

第三题

题目

循环汉诺塔

思路

模拟找规律

代码

cpp 复制代码
#include <iostream>
using namespace std;

const int MOD = 1000000007;

int n;

int main() 
{
    cin >> n;
    int x = 1, y = 2;
    for(int i = 2; i <= n; i++)
    {
        int xx = 2 * y % MOD + 1;
        int yy = (2 * y % MOD + 2 + x) % MOD;
        x = xx;
        y = yy;
    }
    cout << x << " " << y << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")
相关推荐
_OP_CHEN1 天前
【算法基础篇】(四十三)数论之费马小定理深度解析:从同余性质到乘法逆元
c++·算法·蓝桥杯·数论·acm/icpc
水月wwww1 天前
【算法设计】分支限界法
算法·分支限界法
茶猫_1 天前
C++学习记录-旧题新做-链表求和
数据结构·c++·学习·算法·leetcode·链表
yuniko-n1 天前
【牛客面试 TOP 101】链表篇(一)
数据结构·算法·链表·面试·职场和发展
王老师青少年编程1 天前
信奥赛C++提高组csp-s之并查集(案例实践)1
数据结构·c++·并查集·csp·信奥赛·csp-s·提高组
谢娘蓝桥1 天前
adi sharc c/C++ 语言指令优化
开发语言·c++
郑泰科技1 天前
fmm(快速地图匹配)实践:Unknown toolset: vcunk的解决方案
c++·windows·交通物流
2501_941805311 天前
从微服务网关到统一安全治理的互联网工程语法实践与多语言探索
前端·python·算法
源代码•宸1 天前
Leetcode—1161. 最大层内元素和【中等】
经验分享·算法·leetcode·golang
CodeByV1 天前
【算法题】模拟
算法