codeforces round #214 div2 CD

C. Dima and Salad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.

Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words,  , where aj is the taste of the j-th chosen fruit and bj is its calories.

Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!

Inna loves Dima very much so she wants to make the salad from at least one fruit.

Input

The first line of the input contains two integers nk (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integersa1, a2, ..., an (1 ≤ ai ≤ 100) — the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 100) — the fruits' calories. Fruit number i has taste ai and calories bi.

Output

If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.

Sample test(s)
input
3 2
10 8 1
2 7 1
output
18
input
5 3
4 4 4 4 4
2 2 2 2 2
output
-1
Note

In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition  fulfills, that's exactly what Inna wants.

In the second test sample we cannot choose the fruits so as to follow Inna's principle.

 

 

AB就不说了,水题,C其实可以转换为0/1背包求解,具体见代码,注意大于等于0和小于等于0是递推的方向不一样。

/****************************************************
* author:crazy_石头
* Pro:codeforces round #214 div2.C
* algorithm:0/1 Packbag
* Time:15ms
* Judge Status:Accepted
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

const int maxn=100+10;
const int maxm=50000;

int dp[maxm+10];
int a[maxn],b[maxn],n,k;

int main()
{
    cin>>n>>k;
    rep(i,0,n-1) cin>>a[i];
    rep(i,0,n-1)
    {
        cin>>b[i];
        b[i]=a[i]-k*b[i];
    }
    ms(dp,-1),dp[maxm>>1]=0;
    rep(i,0,n-1)
    {
        if(b[i]>=0)
        {
            for(int j=maxm;j>=b[i];j--)
            if(~dp[j-b[i]])
                dp[j]=max(dp[j],dp[j-b[i]]+a[i]);
        }
        else
        {
            for(int j=0;j<=maxm+b[i];j++)
            if(~dp[j-b[i]])
                dp[j]=max(dp[j],dp[j-b[i]]+a[i]);
        }
    }
    printf("%d\n",dp[maxm>>1]!=0?dp[maxm>>1]:-1);
    return 0;
}

 

 

D. Dima and Trap Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal...

Dima constructed a trap graph. He shouted: "Hey Seryozha, have a look at my cool graph!" to get his roommate interested and kicked him into the first node.

A trap graph is an undirected graph consisting of n nodes and m edges. For edge number k, Dima denoted a range of integers from lkto rk (lk ≤ rk). In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let's call it x), then Seryozha must go some way from the starting node with number 1 to the final node with number n. At that, Seryozha can go along edge k only if lk ≤ x ≤ rk.

Seryozha is a mathematician. He defined the loyalty of some path from the 1-st node to the n-th one as the number of integers x, such that if he initially chooses one of them, he passes the whole path. Help Seryozha find the path of maximum loyalty and return to his room as quickly as possible!

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 103, 0 ≤ m ≤ 3·103). Then follow m lines describing the edges. Each line contains four integers akbklk and rk (1 ≤ ak, bk ≤ n, 1 ≤ lk ≤ rk ≤ 106). The numbers mean that in the trap graph the k-th edge connects nodes ak and bk, this edge corresponds to the range of integers from lk to rk.

Note that the given graph can have loops and multiple edges.

Output

In a single line of the output print an integer — the maximum loyalty among all paths from the first node to the n-th one. If such paths do not exist or the maximum loyalty equals 0, print in a single line "Nice work, Dima!" without the quotes.

Sample test(s)
input
4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7
output
6
input
5 6
1 2 1 10
2 5 11 20
1 4 2 5
1 3 10 11
3 4 12 10000
4 5 6 6
output
Nice work, Dima!
Note

Explanation of the first example.

Overall, we have 2 ways to get from node 1 to node 4: first you must go along the edge 1-2 with range [1-10], then along one of the two edges 2-4.

One of them contains range [3-5], that is, we can pass through with numbers 3, 4, 5. So the loyalty of such path is 3.

If we go along edge 2-4 with range [2-7], then we can pass through with numbers 2, 3, 4, 5, 6, 7. The loyalty is 6. That is the answer.

The edge 1-2 have no influence on the answer because its range includes both ranges of the following edges.

 

 

 

/****************************************************
* author:crazy_石头
* Pro:codeforces round #214 div2.D
* algorithm:dfs+枚举+二分
* Time:826ms
* Judge Status:Accepted
* 题意:给定一个图,每个边有限制,必须在[a,b]内,求可以通过的区间最大长度
* 思路:枚举左端点,不断二分右端点,若可行,则缩小范围,dfs判联通
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

const int maxn=10000+10;
const int maxm=1000;

struct E
{
    int to,next;
    int l,r;
}e[maxn];

int head[maxn],vis[maxn],cnt;
bool ok;
int n,m,tot;
int point[maxn];

inline void addedge(int u,int v,int l,int r)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    e[cnt].l=l;
    e[cnt].r=r;
    head[u]=cnt++;
}

inline void add(int u,int v,int l,int r)
{
    addedge(u,v,l,r);
    addedge(v,u,l,r);
}

inline void dfs(int u,int l,int r)
{
    if(ok)return;
    if(u==n)
    {
        ok=1;
        return;
    }
    for(int i=head[u];~i;i=e[i].next)
    {
        int v=e[i].to;
        if(!vis[v]&&e[i].l<=l&&e[i].r>=r)
        {
            vis[v]=1;
            dfs(v,l,r);
        }
    }
}

inline void solve()
{
    int ans=0;
    rep(i,1,tot)
    {
        int l=point[i];
        int r=1000005;
        while(l<r)
        {
            int mid=(l+r)>>1;
            ok=0;
            ms(vis,0);
            vis[1]=1;
            dfs(1,point[i],mid);
            if(ok)l=mid+1;
            else r=mid;
        }
        ans=max(ans,l-point[i]);
    }
    if(ans==0)cout<<"Nice work, Dima!"<<endl;
    else cout<<ans<<endl;
}

int main()
{
    cin>>n>>m;
    ms(head,-1),cnt=0,tot=0;
    int u,v,l,r;
    rep(i,1,m)
    {
        cin>>u>>v>>l>>r;
        point[++tot]=l;
        point[++tot]=r;
        add(u,v,l,r);
    }
    solve();
    return 0;
}


 

E题没看。。不过据说暴搜也可以过,D的思路是显而易见的。D题还有一种方法就是排序+并查集。

 这儿不给出代码,读者自行思考。

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值