codeforces round321 DIV2



A. Kefa and First Steps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.

Help Kefa cope with this task!

Input

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1,  a2,  ...,  an (1 ≤ ai ≤ 109).

Output

Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.

Sample test(s)
Input
6
2 2 1 3 4 1
Output
3
Input
3
2 2 9
Output
3
Note

In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.

In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.


题意:求最长递增子序列的长度。

分析:从每个点开始,一直递增到后面的数比前面的数小,记录每一段递增序列的长度,并不断更新最大长度。


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
const int N=100010;

int a[N];
int main()
{
    int n=in();
    for(int i=0;i<n;i++)
    {
        a[i]=in();
    }
    a[n]=0;
    int ans=0;
    for(int i=0;i<n;i++)
    {
        int cnt=1;    //注意应该设置为1
        for(;i<n;i++)
        {
            if(a[i+1]>=a[i]) cnt++;
            else break;
        }

        ans=max(ans,cnt);
    }
    cout<<ans;
    return 0;
}


B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.

Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

Input

The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.

Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Sample test(s)
Input
4 5
75 5
0 100
150 20
75 1
Output
100
Input
5 100
0 7
11 32
99 10
46 8
87 54
Output
111
Note

In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.

In the second sample test we can take all the friends.

题意:有n个人,每个人有两个参数,要求在满足第一个参数最大值与最小值之间的差距小于d的情况下,求第二个参数和的最大值。

分析:可以先进行排序,然后从第一个人开始,用类似于尺取法的方法,找到最远的满足条件的人,记录这之间的和,到第二个人的时候直接在第一个人的基础上再找最远满足条件的人。


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
const int N=100010;

struct st
{
    int m,s;
}a[N];

bool cmp(const st &a,const st & b)
{
    return a.m<b.m;
}
int main()
{
    int n=in(),d=in();
    for(int i=0;i<n;i++)
    {
        a[i].m=in(),a[i].s=in();
    }
    sort(a,a+n,cmp);
    ll ans=0;
    int j=0;
    ll tmp=0;
    for(int i=0;i<n;i++)
    {
        for(;j<n && a[j].m<a[i].m+d;j++)
        {
            tmp += a[j].s;
        }
        ans=max(ans,tmp);
        tmp-=a[i].s;
    }
    cout<<ans;
    return 0;
}


C. Kefa and Park
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kefa decided to celebrate his first big salary by going to the restaurant.

He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats.

Your task is to help Kefa count the number of restaurants where he can go.

Input

The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.

The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).

Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.

It is guaranteed that the given set of edges specifies a tree.

Output

A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.

Sample test(s)
Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
Output
2
Note

Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.

Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.


题意:给定一棵树,一个人从根节点出发,叶子节点全部都是餐馆,去餐馆的过程中可能有的节点有猫,他不能经过连续的m个存在猫的节点,求可以到达的餐馆数。

分析:直接dfs就行,如果在走的过程中经过的带有猫的节点的数目大于m就返回,碰到叶子节点就更新ans++。叶子节点的判断可以用入度来判断(入度为1,根节点要特判)。


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
const int N=100010;

int have[N];
int rd[N];
vector<int >v[N];
bool vis[N];
int ans;
int n,m;

void dfs(int x,int cnt)
{

    vis[x]=1;
    if(cnt>m) return;
    if(rd[x]==1 && x!=1)
    {
        ans++;
        //cout<<"now=="<<x<<endl;
        return;
    }
    for(int i=0;i<(int)v[x].size();i++)
    {

        int t=v[x][i];

        if(vis[t])continue;
        if(have[t]==1)
        {
            dfs(t,cnt+1);
        }
        else dfs(t,0);
    }
}

int main()
{
    n=in(),m=in();
    for(int i=1;i<=n;i++)
    {
        have[i]=in();
    }
    for(int i=0;i<n-1;i++)
    {
        int t1=in(),t2=in();
        v[t1].push_back(t2);
        v[t2].push_back(t1);
        rd[t1]++,rd[t2]++;
    }
    if(have[1]) dfs(1,1);
    else
        dfs(1,0);
    printf("%d\n",ans);


    return 0;
}


D. Kefa and Dishes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.

Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type — if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c.

Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!

Input

The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.

The second line contains n space-separated numbers ai, (0 ≤ ai ≤ 109) — the satisfaction he gets from the i-th dish.

Next k lines contain the rules. The i-th rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexes i and j (1 ≤ i < j ≤ k), that xi = xj and yi = yj.

Output

In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.

Sample test(s)
Input
2 2 1
1 1
2 1 1
Output
3
Input
4 3 2
1 2 3 4
2 1 5
3 4 2
Output
12
Note

In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.

In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.


题意:有n中食物,每种食物都有一个满意值,同时还有一些附加条件,即某一种食物在另一种食物前面吃的话会增加满意值,求最大的满意值。

分析:从数据就可以看出来是状态压缩dp,用记忆化搜索就可以实现啦。这类题目做的很少,网上搜的题解。。


#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline ll in()
{
    ll res=0;char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
const int N=100010;

ll dp[1<<18][19];
ll add[20][20];
ll a[20];
int n,m,k;

ll DFS(int x,int y,int z)
{
    if(dp[x][y]>=0) return dp[x][y];
    if(z==m) return dp[x][y]=0;
    for(int i=0;i<n;i++)
    {
        if(!(x >> i & 1))     //不再集合中
        {
            dp[x][y]=max(dp[x][y],DFS(x | 1 << i,i,z+1)+add[y][i]+a[i]); //添加到集合中
        }
    }
    return dp[x][y];
}

int main()
{
    mem(dp,-1);
    mem(add,0);
    n=in(),m=in(),k=in();
    for(int i=0;i<n;i++)
    {
        a[i]=in();
    }
    for(int i=0;i<k;i++)
    {
        ll t1=in(),t2=in(),t3=in();
        add[t1-1][t2-1] += t3;
    }
    cout<<DFS(0,n,0);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值