Codeforces Round #421 (Div. 2)

Codeforces Round #421 (Div. 2) 解题报告
本文介绍了Codeforces Round #421 (Div. 2) 中三道题目的解题思路及代码实现:A. Mister B and Book Reading 计算完成阅读所需天数;B. Mister B and Angle in Polygon 寻找正多边形中与指定角度最接近的内角;D. Mister B and PR Shifts 找到使偏差最小的循环移位。

Codeforces Round #421 (Div. 2)

Table of Contents Codeforces Round #421 (Div. 2)A. Mister B and Book ReadingB. Mister B and Angle in PolygonD. Mister B and PR Shifts

A. Mister B and Book Reading

Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.

At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v0 pages, at second — v0 + a pages, at third — v0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than v1 pages per day.

Also, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.

Help Mister B to calculate how many days he needed to finish the book.

Input

First and only line contains five space-separated integers: c, v0, v1, a and l (1 ≤ c ≤ 1000, 0 ≤ l < v0 ≤ v1 ≤ 1000, 0 ≤ a ≤ 1000) — the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.

Output

Print one integer — the number of days Mister B needed to finish the book.

题意:有一本书c页,每天看v0+i*a页,最多看v1页,每次返回l页,几天看完?

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int c,v0,v1,a,l;
    scanf("%d%d%d%d%d",&c,&v0,&v1,&a,&l);
    int ans = 0;
    while(true) {
        ans++;
        c-=v0;
        if(c<=0)
            break;
        v0+=a;
        if(v0>v1)
            v0=v1;
        c+=l;
    }

    printf("%d\n",ans);
    return 0;
}

B. Mister B and Angle in Polygon

On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).

That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value should be minimum possible.

If there are many optimal solutions, Mister B should be satisfied with any of them.

Input

First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.

Output

Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.

题意:正n边形,选三个点,组成的三角形最接近;

分析:求出最小角,180/n

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    double a;
    scanf("%d%lf",&n,&a);

    double k = 180.0/n;

    double ans = k;
    double minx = abs(a-ans);
    int flag = 2;
    for(int i=1;i<n-2;i++) {
        ans+=k;
        if(minx>abs(a-ans)) {
            flag = i+2;
            minx = abs(a-ans);
        }
    }

    printf("%d %d %d\n",1,n,flag);
    return 0;
}

D. Mister B and PR Shifts

Some time ago Mister B detected a strange signal from the space, which he started to study.

After some transformation the signal turned out to be a permutation p of length n or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.

Let's define the deviation of a permutation p as .

Find a cyclic shift of permutation p with minimum possible deviation. If there are multiple solutions, print any of them.

Let's denote id k (0 ≤ k < n) of a cyclic shift of permutation p as the number of right shifts needed to reach this shift, for example:

  • k = 0: shift p1, p2, ... p**n,
  • k = 1: shift pn, p1, ... pn - 1,
  • ...,
  • k = n - 1: shift p2, p3, ... p**n, p1.

Input

First line contains single integer n (2 ≤ n ≤ 106) — the length of the permutation.

The second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of the permutation. It is guaranteed that all elements are distinct.

Output

Print two integers: the minimum deviation of cyclic shifts of permutation p and the id of such shift. If there are multiple solutions, print any of them.

题意:循环队列n次,使得\sum_{i=1}^{n}|p[i]-i| 最小;

分析:和CSUFTOJ1021原理很类似;

记录下来cur移动k次,有多少个正数要变符号,这个cur是一个动态的,最后一个数移动到最前面会产生影响,最后一个数的移走,会对整体要L,R产生影响。

#include<stdio.h>
#include<stdlib.h>
#define LL long long
int p[1000005], cur[2000005];
int main(void)
{
    LL ans, sum;
    int n, L, r, i, temp;
    scanf("%d", &n);
    sum = L = r = temp = 0;
    for(i=1; i<=n; i++)
        scanf("%d", &p[i]);

    for(i=1; i<=n; i++)
    {
        sum += abs(p[i]-i);
        if(p[i]>=i) {
            L++;
            cur[p[i]-i]++;
        }
        else  r++;
    }

    ans = sum;
    for(i=0; i<n-1; i++)
    {
        L -= cur[i];
        r += cur[i];
        sum = sum-L+r-abs(p[n-i]-n-1)+p[n-i]-1;
        cur[p[n-i]+i]++;
        L++, r--;   //最后一个数产生的影响
        if(sum<ans) {
            ans = sum;
            temp = i+1;
        }
    }

    printf("%lld %d\n", ans, temp);
    return 0;
}

转载于:https://www.cnblogs.com/TreeDream/p/7107579.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、付费专栏及课程。

余额充值