【Codeforces809D】Hitchhiking in the Baltic States

本文介绍了一种使用Treap(一种自平衡二叉查找树)来优化动态规划问题的方法。通过对Treap的操作如split和merge,可以高效地解决涉及区间更新和查询的问题。这种方法适用于动态规划中序列严格单调递增的情况。

dp[i] 表示长度为 i 的序列最后一个数的最小值。
考虑转移时加入一段区间 [l,r]
对于最大的满足 dp[i]<l 的位置:

dp[i+1]=min(dp[i+1],l)

因为 dp[i+1]>=l ,所以转移可以变为直接赋值(一定发生)。
对于最大的满足 dp[j]<r 的位置:
dp[k+1]=min(dp[k+1],dp[k]+1),k[i+1,j]

显然 dp 数组严格单调递增,即 dp[i+1]>=dp[i]+1 ,所以上述转移必定发生。
所以我们可以通过一颗非旋转 treap 维护 dp 数组,每次删去 dp[j+1] 这个节点,对于 dp[i+1]dp[j] 这段 dp 区间整体 +1 并右移,再在原来的 dp[i+1] 前加入一个新的 dp[i+1]=l 的节点。这些操作都可以通过 split,merge 较为简单地实现。

#include <bits/stdc++.h>
#define gc getchar()
#define ll long long
#define N 300009
#define inf 0x3f3f3f3f
#define rd(x) (rand()%(x))
using namespace std;
int n,l[N],r[N];
struct node
{
    int size,val,key,add;
    node *lson,*rson;
    node(int v)
    {
        val=v;
        key=rd(100000);
        if (key<=0) key+=100000;
        lson=rson=NULL;
        size=1;
        add=0;
    }
};
typedef node * pnode;
int read()
{
    int x=1;
    char ch;
    while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
    int s=ch-'0';
    while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';
    return s*x;
}
void down(pnode now)
{
    now->val+=now->add;
    if (now->lson) now->lson->add+=now->add;
    if (now->rson) now->rson->add+=now->add;
    now->add=0;
}
pnode merge(pnode L,pnode R)
{
    if (!L) return R;
    if (!R) return L;
    if (L->key>R->key)
    {
        down(L);
        L->rson=merge(L->rson,R);
        return L;
    }
    else
    {
        down(R);
        R->lson=merge(L,R->lson);
        return R;
    }
}
void split(pnode now,int val,pnode &L,pnode &R)
{
    if (!now)
    {
        L=R=NULL;
        return;
    }
    down(now);
    if (now->val>=val)
    {
        split(now->lson,val,L,now->lson);
        R=now;
        return;
    }
    else
    {
        split(now->rson,val,now->rson,R);
        L=now;
        return;
    }
}
int find_begin(pnode now)
{
    down(now);
    if (!now->lson) return now->val;
    return find_begin(now->lson);
}
int get_Ans(pnode now)
{
    if (!now) return 0;
    down(now);
    return get_Ans(now->lson)+get_Ans(now->rson)+(now->val<inf);
}
pnode root,L,M,R,rest;
int main()
{
    n=read();
    for (int i=1;i<=n;i++) l[i]=read(),r[i]=read();
    root=new node(0);
    for (int i=1;i<=n;i++)
        root=merge(root,new node(i+inf));
    for (int i=1;i<=n;i++)
    {
        split(root,l[i],L,R);
        split(R,r[i],M,R);
        if (M) M->add++;
        int Min=find_begin(R);
        split(R,Min+1,rest,R);
        root=merge(L,new node(l[i]));
        root=merge(root,M);
        root=merge(root,R);
    }
    printf("%d\n",get_Ans(root)-1);
    return 0;
}
### Problem Overview The problem "Having Been a Treasurer in the Past, I Help Goblins Deceive" involves determining the minimum number of operations required to adjust a sum of money to a target value, given constraints on how much each operation can change the total. ### Problem Statement Given `n` (maximum number of operations), `k` (target value), and `p` (amount by which the total can be changed per operation), the goal is to compute the minimum number of operations needed to reach the target `k` using steps of size `p`. If it's impossible to reach the target within the given constraints, return `-1`. ### Solution Approach The solution involves a greedy strategy to determine the minimum number of steps required to reach the target. It also checks whether it's possible to achieve the target within the allowed number of operations. ### Key Observations - If the absolute value of the target `k` is greater than `n * p`, it's impossible to reach the target, and the result should be `-1`. - The number of steps required to reach the target can be calculated as the quotient of `|k| / p`, with an additional step if there's a remainder. - This approach ensures that the solution is efficient and adheres to the constraints of the problem. ### Code Implementation ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n, k, p; cin >> n >> k >> p; int res = abs(k) / p; if (abs(k) % p) { res += 1; } if (res > n) { cout << "-1" << endl; } else { cout << res << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; } ``` ### Explanation of the Code - The code reads multiple test cases and processes each one individually. - For each test case, it calculates the minimum number of operations required to reach the target `k` using steps of size `p`. - If the number of operations exceeds `n`, it outputs `-1` to indicate that the target is unreachable. - Otherwise, it outputs the computed number of operations[^2]. ### Example Scenarios Consider the following example: - Input: `n = 5`, `k = 12`, `p = 3` - Output: `4` (since `12 / 3 = 4`) Another example: - Input: `n = 3`, `k = 10`, `p = 3` - Output: `-1` (since `10 / 3 = 3.33`, and `4` operations are needed, which exceeds `n = 3`) ### Time and Space Complexity - **Time Complexity**: `O(1)` per test case, as the computation involves simple arithmetic operations. - **Space Complexity**: `O(1)`, as no additional space is used beyond the input variables. ### Conclusion This problem demonstrates the use of greedy algorithms to solve a practical problem in programming competitions. The approach is both efficient and straightforward, making it suitable for time-constrained environments like contests.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值