Codeforces 294比赛记

本文通过两个具体的模拟问题,展示了如何解决复杂场景下的算法问题。首先介绍了一种模拟鸟类受惊飞行转移过程的方法,并给出了实现代码;其次,针对机器人在棋盘格上按规则行走并涂色的问题,提供了一种高效解决方案及其实现细节。

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass’s territory. Supposed there are ai oskols sitting on the i-th wire.

Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away.

Shaass has shot m birds. You’re given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, …, an, (0 ≤ ai ≤ 100).

The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It’s guaranteed there will be at least yi birds on the xi-th wire at that moment.

Output
On the i-th line of the output print the number of birds on the i-th wire.

Example
Input
5
10 10 10 10 10
5
2 5
3 13
2 12
1 13
4 6
Output
0
12
5
0
16
Input
3
2 4 1
1
2 2
Output
3
0
3
拿到试题先看第一题,水题,果断切。就是强行模拟,1Y

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
ll a[101];
int m,x,y,n;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) cin>>a[i];
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y;
        a[x-1]+=y-1;
        a[x+1]+=(a[x]-y);
        a[x]=0;
    }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<endl;
    }
}

再看第二题,看了一下题目,感觉是搜索,打了几发发现好像可以二分答案+贪心验证,贪心验证比较好想,然后后来发现二分不对,因为余1,余0得分开考虑,看顺序时间复杂度够,就果断顺序扫,然后就过了

Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf’s dimensions to be as small as possible. The thickness of the i-th book is ti and its pages’ width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

Example
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 101
int ans;
int n;
int k2[N];
int l1,l2;
int k1[N];
int l,r=0;
int ho,ku;
bool cmp(int x,int y)
{
    return x>y;
}
bool pd(int x)
{
    int res=0;
    for(int i=0;i<=x/2;i++)
    {
        if(i>l2) break;
        res=0;
        int j=x-2*i;
        if(j>l1) continue;
        if(j<0) break;
        for(int k=i+1;k<=l2;k++) res+=k2[k];
        for(int k=j+1;k<=l1;k++) res+=k1[k];
        if(res<=x) return 1;
    }
    return 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&ho,&ku);
        if(ho==1) k1[++l1]=ku;
        else k2[++l2]=ku;
        r+=ho;
    }
    l=1;
    sort(k1+1,k1+l1+1,cmp);
    sort(k2+1,k2+l2+1,cmp);
//  for(int i=1;i<=l2;i++) cout<<k2[i]<<" ";
//  cout<<r<<endl;
    ans=r;
    for(int i=l;i<=r;i++)
    {
        if(pd(i))
        {
            ans=min(ans,i);
        }
    }
    printf("%d",ans);
}

打完才36min感觉C题一开始看不可做,但是毕竟题目都是有解的,我考虑了一下补集转化,然后数学推了一下,跳了半天才过,过完只剩20min没时间打其他题了,就不再打下去

技巧:考试时极限情况考虑清楚,一定不能慌,求稳

一看排名:RANK 2,还好,主要是我正确率高,不是1Y就是2Y。

然后下午就来刚T4和T5
先刚T5,发现是一个求树的重心的问题(以重心为根的树,最大的儿子size最小:最大的儿子的sz小于一半的所有孩子的sz,单独考虑那条边推一发)
然后就是两遍dfs+容斥求每个点到所有子树中其他点的距离,具体看代码,挺巧妙,然后就是每个点被算了两遍,加的时候加上2*sum最后ans除以2就可以了。这是一道树形DP和树上求值的应用

Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of n·m square tiles forming a n × m rectangle. Therefore he’s decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.

Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (xs, ys) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it’s painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn’t lead to any painting. The first tile the robot is standing on, is also painted.

The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.

Let’s consider an examples depicted below.

If the robot starts at tile number 1 (the tile (1, 1)) of the left grid heading to down-right it’ll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3.

Input
The first line of the input contains two integers n and m, (2 ≤ n, m ≤ 105). The second line contains two integers xs and ys (1 ≤ xs ≤ n, 1 ≤ ys ≤ m) and the direction robot is facing initially. Direction is one of the strings: “UL” (upper-left direction), “UR” (upper-right), “DL” (down-left) or “DR” (down-right).

Note, that record (xs, ys) denotes the tile that is located at the xs-th row from the top and at the ys-th column from the left of the kitchen.

It’s guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles).

Output
Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Example
Input
3 4
1 1 DR
Output
7
Input
3 4
3 3 DR
Output
11
Input
3 3
1 1 DR
Output
-1
Input
3 3
1 2 DL
Output
4

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define forw(i,x) for(int i=fir[x];i;i=ne[i])
#define M 20001
#define N M
LL C[M];
int cnt=1,ne[M],fir[M],to[M],from[M];
int n;
LL f[N];
int S[N];
LL ans =1e18;
int x,y;
LL z;
void dfs(int x,int fa)
{
    f[x]=0;S[x]=1;
    forw(i,x)
    {
        int V=to[i];
        if(V!=fa)
        {
            dfs(V,x);
            S[x]+=S[V];
            f[x]+=f[V]+C[i]*S[V];
        }
    }
}
void add(int x,int y,LL z)
{
    to[++cnt]=y;C[cnt]=z;ne[cnt]=fir[x];fir[x]=cnt;from[cnt]=x;
}
void DFS(int x,int fa,LL &p,int sum)
{
    p=min(p,f[x]);
    forw(i,x)
    {
        int V=to[i];
        if(V!=fa)
        {
            f[to[i]]=f[x]+C[i]*(sum-S[V]*2);
            DFS(V,x,p,sum);
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y>>z;
        add(x,y,z);
        add(y,x,z);
    }
    for(int i=2;i<=cnt;i+=2)
    {
        int U=from[i];int V=to[i];
        dfs(U,V);
        dfs(V,U);
        LL p1=1e18,p2=1e18;
        DFS(U,V,p1,S[U]);DFS(V,U,p2,S[V]);
        long long dance=0;
        for(int j=1;j<=n;j++) dance+=f[j];
        long long it;
        it=dance+2*(S[U]*S[V]*C[i]+p1*S[V]+p2*S[U]);
        ans=min(ans,it);
    }
    cout<<ans/2;
    return 0;
}


然后看D题一道大模拟,然后给我的其实就是:模拟的时候可以考虑边界情况,会让复杂的模拟变简单!这里就是考虑它触边(n+m-2)次时就行了,搞了2*(n+m-2)次还不行,就说明无解

Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of n·m square tiles forming a n × m rectangle. Therefore he’s decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.

Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (xs, ys) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it’s painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn’t lead to any painting. The first tile the robot is standing on, is also painted.

The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.

Let’s consider an examples depicted below.

If the robot starts at tile number 1 (the tile (1, 1)) of the left grid heading to down-right it’ll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3.

Input
The first line of the input contains two integers n and m, (2 ≤ n, m ≤ 105). The second line contains two integers xs and ys (1 ≤ xs ≤ n, 1 ≤ ys ≤ m) and the direction robot is facing initially. Direction is one of the strings: “UL” (upper-left direction), “UR” (upper-right), “DL” (down-left) or “DR” (down-right).

Note, that record (xs, ys) denotes the tile that is located at the xs-th row from the top and at the ys-th column from the left of the kitchen.

It’s guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles).

Output
Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Example
Input
3 4
1 1 DR
Output
7
Input
3 4
3 3 DR
Output
11
Input
3 3
1 1 DR
Output
-1
Input
3 3
1 2 DL
Output
4

//神模拟
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<new>
using namespace std;
bool w[4][1000001];
long long ans;
int cnt,tot;
int x,y;
int dx,dy;
string ty;
int n,m;
bool flag=0;
void doit(int x,int y)
{
    int a,b;
    if(x==1) a=0,b=y;else if(x==n) a=1,b=y;else if(y==1) a=2,b=x;else a=3,b=x;
    if(!w[a][b])w[a][b]=1,tot++;
    if(x==1) dx=1;if(x==n) dx=-1;if(y==1) dy=1;if(y==m) dy=-1;
}
int main()
{
    cin>>n>>m;
    cin>>x>>y>>ty;
    if(ty[0]=='U')dx=-1;else dx=1;
    if(ty[1]=='L')dy=-1;else dy=1;
    ans=1;tot=1;
    for(int i=1;i<=2*(n+m-2);i++)
    {
        doit(x,y);
        if(flag){puts("-1");return 0;}
        if(tot>=n+m-1) {cout<<ans<<endl;return 0;}
        int ww=min(dx>0?n-x:x-1,dy>0?m-y:y-1);
        x+=dx*ww;y+=dy*ww;ans+=ww;
    }
    puts("-1");
} 
AI 代码审查Review工具 是一个旨在自动化代码审查流程的工具。它通过集成版本控制系统(如 GitHub 和 GitLab)的 Webhook,利用大型语言模型(LLM)对代码变更进行分析,并将审查意见反馈到相应的 Pull Request 或 Merge Request 中。此外,它还支持将审查结果通知到企业微信等通讯工具。 一个基于 LLM 的自动化代码审查助手。通过 GitHub/GitLab Webhook 监听 PR/MR 变更,调用 AI 分析代码,并将审查意见自动评论到 PR/MR,同时支持多种通知渠道。 主要功能 多平台支持: 集成 GitHub 和 GitLab Webhook,监听 Pull Request / Merge Request 事件。 智能审查模式: 详细审查 (/github_webhook, /gitlab_webhook): AI 对每个变更文件进行分析,旨在找出具体问题。审查意见会以结构化的形式(例如,定位到特定代码行、问题分类、严重程度、分析和建议)逐条评论到 PR/MR。AI 模型会输出 JSON 格式的分析结果,系统再将其转换为多条独立的评论。 通用审查 (/github_webhook_general, /gitlab_webhook_general): AI 对每个变更文件进行整体性分析,并为每个文件生成一个 Markdown 格式的总结性评论。 自动化流程: 自动将 AI 审查意见(详细模式下为多条,通用模式下为每个文件一条)发布到 PR/MR。 在所有文件审查完毕后,自动在 PR/MR 中发布一条总结性评论。 即便 AI 未发现任何值得报告的问题,也会发布相应的友好提示和总结评论。 异步处理审查任务,快速响应 Webhook。 通过 Redis 防止对同一 Commit 的重复审查。 灵活配置: 通过环境变量设置基
【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器的状态空间平均模型的建模策略。该方法通过数学建模手段对直流微电网系统进行精确的状态空间描述,并对其进行线性化处理,以便于系统稳定性分析与控制器设计。文中结合Matlab代码实现,展示了建模与仿真过程,有助于研究人员理解和复现相关技术,推动直流微电网系统的动态性能研究与工程应用。; 适合人群:具备电力电子、电力系统或自动化等相关背景,熟悉Matlab/Simulink仿真工具,从事新能源、微电网或智能电网研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网的动态建模方法;②学习DC-DC变换器在耦合条件下的状态空间平均建模技巧;③实现系统的线性化分析并支持后续控制器设计(如电压稳定控制、功率分配等);④为科研论文撰写、项目仿真验证提供技术支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐步实践建模流程,重点关注状态变量选取、平均化处理和线性化推导过程,同时可扩展应用于更复杂的直流微电网拓扑结构中,提升系统分析与设计能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值