当前位置: 首页 > article >正文

图论BFS

D1. The Endspeaker (Easy Version)

time limit per test

2 seconds

memory limit per test

256 megabytes

This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.

You're given an array aa of length nn, and an array bb of length mm (bi>bi+1bi>bi+1 for all 1≤i<m1≤i<m). Initially, the value of kk is 11. Your aim is to make the array aa empty by performing one of these two operations repeatedly:

  • Type 11 — If the value of kk is less than mm and the array aa is not empty, you can increase the value of kk by 11. This does not incur any cost.
  • Type 22 — You remove a non-empty prefix of array aa, such that its sum does not exceed bkbk. This incurs a cost of m−km−k.

You need to minimize the total cost of the operations to make array aa empty. If it's impossible to do this through any sequence of operations, output −1−1. Otherwise, output the minimum total cost of the operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤3⋅1051≤n,m≤3⋅105, 1≤n⋅m≤3⋅1051≤n⋅m≤3⋅105).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

The third line of each test case contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤1091≤bi≤109).

It is also guaranteed that bi>bi+1bi>bi+1 for all 1≤i<m1≤i<m.

It is guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 3⋅1053⋅105.

Output

For each test case, if it's possible to make aa empty, then output the minimum total cost of the operations.

If there is no possible sequence of operations which makes aa empty, then output a single integer −1−1.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cout << "[debug]" << " = " << x << '\n'
typedef std::pair<int,int> pii;
const int N = 15 + 5, MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n+1);
    map<ll, int> visit;
    map <ll, vector<ll>> mp;
    for(ll i = 1; i <= n; i ++){
        cin >> a[i];
        ll u = a[i] + i - 1;
        ll v = u + i - 1;
        mp[u].push_back (v);
    }
    ll max = n;
    queue<ll> q;
    for(auto i: mp[n]) {
        q.push(i);
        if(i > max) max = i;
        visit[i] =1;
    }
    while (!q.empty()) {
        ll j = q.front();
        q.pop();
        for(auto i: mp[j]) {
            if(visit[i] == 0) {
                q.push(i);
                if(i > max) max = i;
                visit[i] =1;
            }
        }
    }
    cout << max << '\n';
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0);
    //std::cout << std::fixed << std::setprecision(2);
    int T = 1;
    std::cin >> T;
    while(T --) solve();
    return 0;
}

div2 的c题 使用数组转化成图论问题,找出节点最大值,因为其中的数值较大,使用map存储路径和访问节点,遍历时间复杂度为NlogN。


http://www.kler.cn/a/374498.html

相关文章:

  • day13:FTP服务
  • cgroup2如何切换成 cgroup1
  • C++知识点复习
  • RNN在训练中存在的问题
  • QT:子线程更新UI
  • C++学习笔记3——存储持续性、作用域和链接性
  • 微信小程序之流浪动物救助:爱与希望同行
  • 【SQL】 Navicate 17 连接sql server
  • 【小白学机器学习31】 大数定律,中心极限定理,标准正态分布与概率的使用
  • Vue2指令原理手写
  • 基于SSM+微信小程序的汽车预约维修管理系统(汽车3)
  • sublime text 常用快捷键
  • Chrome与夸克谁更节省系统资源
  • 宝塔使用clickhouse踩坑
  • 《AI产品经理手册》——解锁AI时代的商业密钥
  • 从新手到专家:7款电脑平面设计软件评测
  • WPF+MVVM案例实战(十五)- 实现一个下拉式菜单(上)
  • OpenCV视觉分析之目标跟踪(3)实现基于金字塔的 Lucas-Kanade 算法来进行稀疏光流计算的类SparsePyrLKOpticalFlow的使用
  • 《解锁 TDD 魔法:高效软件开发的利器》
  • 读写chrome.storage.local
  • 股票基础交易规则——涨跌幅限制、价格笼子?
  • Java:阿里云联络中心“双呼A”功能系统接入
  • vscode | 开发神器vscode快捷键删除和恢复
  • 【VM实战】VMware迁移到VirtualBox
  • 无人机之感知避让技术篇
  • 【网络】什么是 ICMP (Internet Control Message Protocol)?