Best Cow Fences POJ - 2018(经典二分)

本文介绍了一种解决最大化平均值围栏问题的方法,通过枚举可能的平均值并使用二分查找法来确定最优解。该问题要求在一个包含不同数量奶牛的田地中找出连续的一段田地,使得这段田地的平均奶牛数最大,同时这段田地的数量至少为给定值。

Farmer John’s farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.

FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.

Calculate the fence placement that maximizes the average, given the constraint.
Input
* Line 1: Two space-separated integers, N and F.

  • Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.
    Output
  • Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields.
    Sample Input
    10 6
    6
    4
    2
    10
    3
    8
    5
    9
    4
    1
    Sample Output
    6500
    评价:题意是在一个数组里,寻找一段连续和,使其平均和最大,但是长度不能小于F,
    首先可以看出是满足单调性的,但是怎么二分呢,
    我们先枚举一个可能的数。
    然后数组里的值全部减去这个值(结果会有正有负),那么我们就看是否存一段长度大于等于F,且和为正。对于此的判断,可谓经典,见代码。
    AC
#include<iostream>
#include<cstdio>
#include<vector>
#define maxx 400005
using namespace std;
double a[100005];
double b[100005];
double c[100005];
int main()
{

        int n;
        int f;
        cin>>n>>f;
        for(int i=1;i<=n;i++)
            scanf("%lf",a+i);
        double eps=1e-5;
        double l=-1e6,r=1e6;
        while(r-l>eps)
        {
            double mid=(l+r)/2;
            for(int i=1;i<=n;i++)b[i]=a[i]-mid;
            for(int i=1;i<=n;i++)
                c[i]=c[i-1]+b[i];//求前缀和,
            double ans=-1e10;
            double minn=1e10;
            for(int i=f;i<=n;i++)
            {
                minn=min(minn, c[i-f]);不断维护左端的最小值
                ans=max(ans, c[i]-minn);//相见得到区间和,不断忘正值靠近
            }//核心
            if(ans>=0)
                l=mid;
            else
                r=mid;
        }
        cout<<(int)(r*1000);
    return 0;
}
`------------Active Fences Info------------` 是 Android 系统中 **SurfaceFlinger** 或 **GPU 子系统** 输出的一个调试信息块,用于显示当前系统中正在使用的 **Fence(同步栅栏)** 对象。这些 Fence 通常用于 **跨进程、跨硬件(CPU/GPU)的图形同步操作**,确保图形缓冲区在正确的时间被读取或写入,避免画面撕裂或数据竞争。 --- ### **什么是 Fence?** 在 Android 图形系统中,**Fence** 是一种同步机制,通常基于 Linux 的 **sync_file** 或 **fence** 机制实现。它用于协调不同组件(如 GPU、HWC、SurfaceFlinger)对图形缓冲区的访问。 #### Fence 的主要用途: - 同步 **GPU 渲染完成** 与 **SurfaceFlinger 合成** - 控制 **VSync** 信号的同步 - 避免缓冲区被重复写入或提前释放 - 支持 **硬件合成器(HWC)** 的异步合成 --- ### **Active Fences Info 输出示例** 执行 `adb dumpsys SurfaceFlinger` 后可能会看到如下输出: ``` ------------Active Fences Info------------ Count: 5 Fence[0]: name="ReleaseFence" status=signaled time=1234567890 Fence[1]: name="AcquireFence" status=pending time=1234567895 Fence[2]: name="VSync" status=signaled time=1234567900 Fence[3]: name="GPU_CMD" status=pending time=1234567905 Fence[4]: name="HWC_LAYER" status=signaled time=1234567910 ``` --- ### **字段解析** | 字段 | 含义 | |------|------| | `name` | Fence 的名称,表示其用途,如 AcquireFence、ReleaseFence、VSync 等 | | `status` | 当前状态:<br> - `signaled`:已触发,同步完成<br> - `pending`:等待触发 | | `time` | Fence 创建或触发的时间戳(单位通常为纳秒) | --- ### **常见 Fence 类型** | 类型 | 说明 | |------|------| | `AcquireFence` | 表示图层缓冲区何时准备好供 SurfaceFlinger 使用 | | `ReleaseFence` | 表示 SurfaceFlinger 已完成对缓冲区的使用 | | `VSync` | 用于同步 VSync 信号,控制刷新节奏 | | `GPU_CMD` | GPU 完成绘图命令后触发的同步信号 | | `HWC_LAYER` | HWC 合成层的同步信号 | --- ### **调试与分析用途** - **画面卡顿排查**:检查是否有 Fence 长时间处于 `pending` 状态。 - **缓冲区竞争问题**:查看 Acquire/Release Fence 是否频繁延迟。 - **GPU 或 HWC 性能瓶颈**:通过 Fence 时间戳分析渲染延迟。 - **同步逻辑验证**:确认 Fence 的使用是否符合预期流程。 - **死锁或资源泄露**:长期未触发的 Fence 可能表示同步异常。 --- ### **常用调试命令** ```bash adb dumpsys SurfaceFlinger | grep -A 50 "Active Fences Info" adb shell dumpsys SurfaceFlinger --fences ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值