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

题目链接: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.csdn.net/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.csdn.net/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.csdn.net/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]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值