Codeforces Round #377 (Div. 2)

Codeforces D.Exams 解析
本文解析了Codeforces上D.Exams问题的解决方法,采用二分查找确定完成所有考试所需的最少天数。该问题涉及输入处理、数组操作及条件判断等编程技巧。

D. Exams
题目http://codeforces.com/problemset/problem/732/D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.

About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can’t pass any exam. It is not allowed to pass more than one exam on any day.

On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.

About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.

Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.

The second line contains n integers d1, d2, …, dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.

The third line contains m positive integers a1, a2, …, am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
Output

Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
Examples
Input

7 2
0 1 0 2 1 0 2
2 1

Output

5

Input

10 3
0 0 1 2 3 0 2 0 1 2
1 1 4

Output

9

Input

5 1
1 1 1 1 1
5

Output

-1


用二分查找确定需要的天数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo1(i,n) for(int i = 1; i <= n; ++i)
#define fo0(i,n) for(int i = 0; i < n; ++i)
using namespace std;
const int LEN = 1e5+4;
int n,m;
int d[LEN];
int e[LEN];
int flag[LEN];
bool check(int ans)
{
   memset(flag,0,sizeof(flag));
   int res = 0;//用来标记需要的时间
   int exam = m;//用来标记考试的数目
   for(int i = ans; i >= 1; --i)//关键是从后往前找
   {
       if(d[i]&&!flag[d[i]])
       {
           res += e[d[i]];
           flag[d[i]]= 1;
           --exam;
       }
       else
       {
           if(res>0)
           --res;
       }
   }
   if(res>0||exam>0)
    return false;
   else
    return true;
}
int main(void)
{

    cin>>n>>m;
    fo1(i,n)
    scanf("%d",&d[i]);
    fo1(i,m)
    scanf("%d",&e[i]);
    int Left = 0,Right = n;
    int Mid;
    while(Right>Left)
    {
        Mid = Left+(Right-Left)/2;
        if(check(Mid))
         Right = Mid;
        else
         Left = Mid+1;
    }
    if(check(Right))
     cout<<Right<<endl;
    else
     cout<<-1<<endl;

    return 0;
}

转载于:https://www.cnblogs.com/zzuzxy/p/8542661.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值