codeforces Codeforces Round #407 Div2题解 B,C,D,E

本文提供Codeforces竞赛789题目的详细解答过程,包括Masha and Geometric Depression、Functions Again等题目的解析及代码实现,涉及最大子段和算法、欧拉路径等知识点。

题目链接:about:http://codeforces.com/contest/789

B仔细想一想,对于一个l r 区间的最大值,其实就是求一个最大连续子序列。

不过这个地方有点特殊,区间都是以开头为正数的。然后正负正负。

但仔细想想就会发现其实就只有两种情况:

对于这整个差的绝对值得序列:

1.正负正负…….

2.负正负正……

所以我们构造这样的两个序列,求这两个序列的最大子序列。 . Masha and geometric depression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.

You are given geometric progression b defined by two integers b1 and q. Remind that a geometric progression is a sequence of integersb1, b2, b3, …, where for each i > 1 the respective term satisfies the condition bi = bi - 1·q, where q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b1 and q can equal 0. Also, Dvastan gave Masha m “bad” integersa1, a2, …, am, and an integer l.

Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is satisfied (|x| means absolute value of x). There is an exception: if a term equals one of the “bad” integers, Masha skips it (doesn’t write onto the board) and moves forward to the next term.

But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print “inf” in case she needs to write infinitely many integers.
Input

The first line of input contains four integers b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 1 ≤ m ≤ 105) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of “bad” integers, respectively.

The second line contains m distinct integers a1, a2, …, am (-109 ≤ ai ≤ 109) — numbers that will never be written on the board.
Output

Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or “inf” (without quotes) otherwise.
Examples
input

3 2 30 4
6 14 25 48

output

3

input

123 1 2143435 4
123 11 -5453 141245

output

0

input

123 1 2143435 4
54343 -13 6 124

output

inf

Note

In the first sample case, Masha will write integers 3, 12, 24. Progression term 6 will be skipped because it is a “bad” integer. Terms bigger than 24 won’t be written because they exceed l by absolute value.

In the second case, Masha won’t write any number because all terms are equal 123 and this is a “bad” integer.

In the third case, Masha will write infinitely integers 123.

    #include<bits/stdc++.h>  
    #define ll __int64  
    using namespace std;  
    ll b1,q,l,m;  
    ll tmp;  
    map<ll,int>mp;  
    ll ans=0;  
    int main()  
    {  
        scanf("%I64d%I64d%I64d%I64d",&b1,&q,&l,&m);  
        for(ll i=1;i<=m;i++){  
            scanf("%I64d",&tmp);  
            mp[tmp]=1;  
        }  
        for(int i=1;i<=1000;i++)ans=0;  
        for(int i=0;i<501;i++){  
            if(abs(b1)>l)break;  
            if(mp[b1]!=1)ans++;  
            b1*=q;  
        }  
        if(ans>100)printf("inf\n");  
        else printf("%I64d\n",ans);  
        return 0;  
    }  

C. Functions again
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Something happened in Uzhlyandia again… There are riots on the streets… Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input

The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

The second line contains n integers a1, a2, …, an (-109 ≤ ai ≤ 109) — the array elements.
Output

Print the only integer — the maximum value of f.
Examples
input

5
1 4 2 3 1

output

3

input

4
1 5 4 7

output

6

Note

In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.

参考http://blog.youkuaiyun.com/qq_33362864/article/details/68490769

最大字段和裸题...
1.正负正负.......

2.负正负正......

所以我们构造这样的两个序列,求这两个序列的最大子序列。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define LL long long
using namespace std;
LL a[300000];
LL b[300000];
LL c[300000];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%I64d",&a[i]);
    }
    a[n]=0;
    for(int i=0;i<n;i++){
        b[i]=abs(a[i]-a[i+1]);
        if(i&1)
            b[i]=-b[i];
        c[i]=-b[i];
    }
    LL sum=0,mic=0;
    for(int i=0;i<n-1;i++){
        sum+=b[i];
        if(sum<0){
            sum=0;
        }
        mic=max(mic,sum);
    }
    sum=0;
    for(int i=0;i<n-1;i++){
        sum+=c[i];
        if(sum<0){
            sum=0;
        }
        mic=max(mic,sum);
    }
    mic=max(sum,mic);
    cout<<mic<<endl;
    return 0;
}

D:

题目大意:

给你N个点,M条边,让你从一个点出发,遍历所有边,其中只有两条边只遍历了一次,其余的都遍历了两次。
Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor — calculate the number of good paths.

问一共有多少种可行方案。

Examples
Input

5 4
1 2
1 3
1 4
1 5

Output

6

Input

5 3
1 2
2 3
4 5

Output

0

Input

2 2
1 1
1 2

Output

1

Note

In first sample test case the good paths are:

    2 → 1 → 3 → 1 → 4 → 1 → 5,
    2 → 1 → 3 → 1 → 5 → 1 → 4,
    2 → 1 → 4 → 1 → 5 → 1 → 3,
    3 → 1 → 2 → 1 → 4 → 1 → 5,
    3 → 1 → 2 → 1 → 5 → 1 → 4,
    4 → 1 → 2 → 1 → 3 → 1 → 5. 

There are good paths that are same with displayed above, because the sets of roads they pass over once are same:

    2 → 1 → 4 → 1 → 3 → 1 → 5,
    2 → 1 → 5 → 1 → 3 → 1 → 4,
    2 → 1 → 5 → 1 → 4 → 1 → 3,
    3 → 1 → 4 → 1 → 2 → 1 → 5,
    3 → 1 → 5 → 1 → 2 → 1 → 4,
    4 → 1 → 3 → 1 → 2 → 1 → 5,
    and all the paths in the other direction. 

Thus, the answer is 6.

In the second test case, Igor simply can not walk by all the roads.

In the third case, Igor walks once over every road.

参考http://blog.youkuaiyun.com/harlow_cheng/article/details/70335728

首先不能忘了性质...
连通的无向图有欧拉路径的充要条件是:

    **G中奇顶点(连接的边数量为奇数的顶点)的数目等于0或者2。**
   把每条边都复制一条相同的边;
        然后问题就能转化为在2*m条边中,去掉两条边;
        然后使得剩下的图能够进行一笔画(每条边都只经过一次)
        则使奇点的个数为0或为2就好了;
        考虑自环边和普通边;
        对于普通边来说:
        ①如果删掉的两条普通边是不相邻的两条边;
        那么会有4个点变成奇点->排除
        ②如果删掉的两条普通边是相邻的两条边
        则x-y-z中x和z会变成奇点;y仍旧是偶点;
        刚好形成了两个奇点->符合要求
        ③考虑两条不同的自环边,如果删掉之后
            每条自环边的端点两边都是同一个点,则每个点度数都减少2;
            则每个点还都是偶点->奇点个数为0->符合
        ④一条自环边和一条普通边
            普通边两边的点都变成奇点
            ->自环边两边的点是同一个点那个点度数-2还是偶点;
            ->总共两个奇点
            还是符合题意
        综上只要考虑
        自环边和自环边
        相邻的普通边
        自环边和普通边
        3种情况
        如果没有联通的话得输出0
        这里的联通只考虑m条边中出现过的点哦;
        只要那些点联通就可以了
  ans=(long long)loop*(m-loop)+(long long)loop*(loop-1)/2;  
    for(int i=1;i<=n;i++)  
        ans+=(long long)de[i]*(de[i]-1)/2;  
    printf("%I64d",ans);  
    return 0;  

Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)

Examples
input

400 4
100 300 450 500

output

2

input

50 2
100 25

output

3

Note

In the first sample case, we can achieve concentration using one liter of Coke of types and : .

In the second case, we can achieve concentration using two liters of type and one liter of type: .

参考:http://blog.youkuaiyun.com/mengxiang000000/article/details/68961421

目大意:

给你K种浓度的药水,让你配出浓度为N的药水、

问最少需要用多少个药水,一种药水可以无限次使用。


思路:


1、要求出浓度为N的药水 ,我们不妨先将每种药水都减去N,那么问题就变成了凑0.


2、那么要怎么凑0呢?

我们设定dp【i】表示浓度为i的药水最少配成需要的药水个数,那么只要设定大小为2000+即可。

然后确定一点,ai<=1000,那么药水的种类最多也就是1000种那么我们此时Bfs维护dp数组的时间复杂度就是O1000*2000);

可以轻松Ac.
由于ai≤1000,和不会超出1000,(超出,就不会再为0了),画图理解下

所以这个关键是状态图的转移,还有就是题目的转化…
然而下面这个代码会T.. (因为k==1e6,可能导致每次转移太多了.实际上就只需要1000..

int a[N];
int dp[N];
vector<int>vec;
int n,k;
map<int,int>mp;
void spfa(){
    queue<int>q;
    for(int i=1;i<=k;++i){
        int x=n-a[i];
        if(!mp[x])vec.push_back(x);
        x+=1000;
        dp[x]=1;
        if(!mp[x])q.push(x);
        mp[x]=1;
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        int sz=vec.size();
        for(int i=0;i<sz;++i){
            int nx=vec[i]+u;
            if(nx>=0&&nx<=2000&&dp[nx]>dp[u]+1){
                dp[nx]=dp[u]+1;
                if(nx==1000)break;
                q.push(nx);
            }
        }
    }
}
int main(){
    sf("%d%d",&n,&k);
    rep(i,1,k){ sf("%d",&a[i]); }
    rep(i,0,3000)dp[i]=INF;
    spfa();
    if(dp[1000]==INF)puts("-1");
    else pf("%d\n",dp[1000]);
}

AC代码

int a[N*1000];
int dp[N];
vector<int>vec;
int n,k;
int in[N];
void spfa(){
    queue<int>q;
    for(int i=0;i<=1000;++i){
        if(in[i]){
            vec.push_back(n-i);
            int x=n-i+1000;
            dp[x]=1;
            q.push(x);
        }
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        int sz=vec.size();
        for(int i=0;i<sz;++i){
            int nx=vec[i]+u;
            if(nx>=0&&nx<=2000&&dp[nx]>dp[u]+1){
                dp[nx]=dp[u]+1;
                if(nx==1000)break;//
                q.push(nx);
            }
        }
    }
}
int main(){
    //ree
    sf("%d%d",&n,&k);
    rep(i,1,k){ sf("%d",&a[i]); in[a[i]]=1;}
    rep(i,0,3000)dp[i]=INF;
    spfa();
    if(dp[1000]==INF)puts("-1");
    else pf("%d\n",dp[1000]);
}
【多变量输入超前多步预测】基于CNN-BiLSTM的光伏功率预测研究(Matlab代码实现)内容概要:本文介绍了基于CNN-BiLSTM模型的多变量输入超前多步光伏功率预测方法,并提供了Matlab代码实现。该研究结合卷积神经网络(CNN)强大的特征提取能力与双向长短期记忆网络(BiLSTM)对时间序列前后依赖关系的捕捉能力,构建了一个高效的深度学习预测模型。模型输入包含多个影响光伏发电的气象与环境变量,能够实现对未来多个时间步长的光伏功率进行精确预测,适用于复杂多变的实际应用场景。文中详细阐述了数据预处理、模型结构设计、训练流程及实验验证过程,展示了该方法相较于传统模型在预测精度和稳定性方面的优势。; 适合人群:具备一定机器学习和深度学习基础,熟悉Matlab编程,从事新能源预测、电力系统分析或相关领域研究的研发人员与高校研究生。; 使用场景及目标:①应用于光伏电站功率预测系统,提升电网调度的准确性与稳定性;②为可再生能源并网管理、能量存储规划及电力市场交易提供可靠的数据支持;③作为深度学习在时间序列多步预测中的典型案例,用于科研复现与教学参考。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注数据归一化、CNN特征提取层设计、BiLSTM时序建模及多步预测策略的实现细节,同时可尝试引入更多外部变量或优化网络结构以进一步提升预测性能。
### 完美排列问分析 对于 Codeforces Round 1007 (Div. 2) 中的 **B. Perfecto** 问,目标是找到一个长度为 \( n \) 的完美排列。如果这样的排列存在,则输出该排列;否则输出 `-1`。 #### 目解析 目定义了一个“完美排列”,其条件如下: - 对于任意位置 \( i \),满足 \( |p_i - p_{i+1}| = 1 \) 或者 \( |p_i - p_{i+1}| = n-1 \)[^2]。 这意味着相邻两个元素之间的差值要么等于 1(即连续),要么等于 \( n-1 \)(即首尾相连)。 ####方法 通过观察和归纳可以得出以下结论: - 当 \( n \% 3 == 0 \) 或 \( n \% 3 == 2 \) 时,无法构建出符合上述条件的完美排列。 - 而当 \( n \% 3 == 1 \) 时,可以通过特定构造方式生成所需排列。 具体实现逻辑如下: ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n % 3 != 1) { // 如果不符合模数条件 cout << "-1\n"; return; } vector<int> res(n); bool flag = true; // 控制交替模式 for(int i = 0;i < n;i++) { if(flag){ res[i] = i + 1; } else{ res[i] = ((n-i)+1)%n; if(res[i]==0)res[i]=n; } flag=!flag; } for(auto num : res){ cout<<num<<" "; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t=1; while(t--){ solve(); } } ``` 此代码片段实现了基于输入大小 \( n \) 来判断是否存在合法解并输出相应结果的功能。 #### 关键点说明 - 判断依据来源于对不同余数值下能否形成循环结构的研究成果。 - 构造过程中采用交替填充策略来确保最终序列能够满足绝对差的要求。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值