Week Part3:2014/11/16

来源:互联网 发布:淘宝旺铺50块钱交不交 编辑:程序博客网 时间:2024/05/05 15:38

A;水题:

B:不会:

C:水题;

D:动态规划:ZOJ 3623

Battle Ships

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce Nkinds of battle ships. The factory takes ti seconds to produce the i-th battle ship and this battle ship can make the tower loss li longevity every second when it has been produced. If the longevity of the tower lower than or equal to 0, the player wins. Notice that at each time, the factory can choose only one kind of battle ships to produce or do nothing. And producing more than one battle ships of the same kind is acceptable.

Your job is to find out the minimum time the player should spend to win the game.

Input

There are multiple test cases. 
The first line of each case contains two integers N(1 ≤ N ≤ 30) and L(1 ≤ L ≤ 330), N is the number of the kinds of Battle Ships, L is the longevity of the Defense Tower. Then the followingN lines, each line contains two integers i(1 ≤ i ≤ 20) and li(1 ≤ li ≤ 330) indicating the produce time and the lethality of the i-th kind Battle Ships.

Output

Output one line for each test case. An integer indicating the minimum time the player should spend to win the game.

Sample Input

1 11 12 101 12 53 1001 103 2010 100

Sample Output

245
考虑dp[j]指时间为j时的结果.有dp[j+t[i]]=max(dp[j]+l[i]*j,dp[j+t[i]]);

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<cmath>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=350;int t[25],l[25];int dp[maxn];int n,L;int main(){    while(cin>>n>>L)    {        REPF(i,1,n)  cin>>t[i]>>l[i];        CLEAR(dp,0);        REPF(j,1,L)        {            REPF(i,1,n)            dp[j+t[i]]=max(dp[j]+l[i]*j,dp[j+t[i]]);        }        REPF(i,1,maxn)        {            if(dp[i]>=L)            {                printf("%d\n",i);                break;            }        }    }    return 0;}

E:HDU 3080 

The plan of city rebuild

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 692    Accepted Submission(s): 237


Problem Description
News comes!~City W will be rebuilt with the expectation to become a center city. There are some villages and roads in the city now, however. In order to make the city better, some new villages should be built and some old ones should be destroyed. Then the officers have to make a new plan, now you , as the designer, have the task to judge if the plan is practical, which means there are roads(direct or indirect) between every two villages(of course the village has not be destroyed), if the plan is available, please output the minimum cost, or output"what a pity!".
 

Input
Input contains an integer T in the first line, which means there are T cases, and then T lines follow.
Each case contains three parts. The first part contains two integers l(0<l<100), e1, representing the original number of villages and roads between villages(the range of village is from 0 to l-1), then follows e1 lines, each line contains three integers a, b, c (0<=a, b<l, 0<=c<=1000), a, b indicating the village numbers and c indicating the road cost of village a and village b . The second part first contains an integer n(0<n<100), e2, representing the number of new villages and roads(the range of village is from l to l+n-1), then follows e2 lines, each line contains three integers x, y, z (0<=x, y<l+n, 0<=z<=1000), x, y indicating the village numbers and z indicating the road cost of village x and village y. The third part contains an integer m(0<m<l+n), representing the number of deserted villages, next line comes m integers, p1,p2,…,pm,(0<=p1,p2,…,pm<l+n) indicating the village number. 
Pay attention: if one village is deserted, the roads connected are deserted, too.
 

Output
For each test case, If all villages can connect with each other(direct or indirect), output the minimum cost, or output "what a pity!".
 

Sample Input
24 50 1 100 2 202 3 401 3 101 2 701 14 1 6022 33 30 1 202 1 402 0 702 30 3 101 4 902 4 1000
 

Sample Output
70160
 

Author
wangjing
 
标记下,水的最小生成树。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;const int maxn=1100;int mp[maxn][maxn];int vis[maxn],low[maxn];int d[maxn];int t,n,m;int u,v;void Prim(){    int pos;    LL ans=0;    CLEAR(vis,0);    CLEAR(low,INF);    REP(i,n)    {        if(!d[i])        {            low[i]=0;            break;        }    }    REP(i,n)    {        pos=-1;        REP(j,n)        {            if(!d[j]&&!vis[j]&&(pos==-1||low[j]<low[pos]))                pos=j;        }        ans+=(LL)low[pos];        vis[pos]=1;        REP(j,n)        {            if(!vis[j]&&!d[j]&&low[j]>mp[pos][j])                low[j]=mp[pos][j];        }    }    if(ans<INF)   cout<<ans<<endl;    else   cout<<"what a pity!"<<endl;}int main(){    std::ios::sync_with_stdio(false);    cin>>t;    int x,y,w;    while(t--)    {        cin>>n>>m;        CLEAR(mp,INF);        CLEAR(d,0);        REP(i,m)        {            cin>>x>>y>>w;            if(mp[x][y]>w)                mp[x][y]=mp[y][x]=w;        }        cin>>u>>v;        n+=u;        REP(i,v)        {            cin>>x>>y>>w;            if(mp[x][y]>w)                mp[x][y]=mp[y][x]=w;        }        cin>>m;        while(m--)        {            cin>>x;            d[x]=1;        }        Prim();    }    return 0;}

H:CF 428B 线段树,向下更新要或,向上更新要与,
B. Interesting Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers liriqi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn't exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

Input

The first line contains two integers nm (1 ≤ n ≤ 1051 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers liriqi (1 ≤ li ≤ ri ≤ n0 ≤ qi < 230) describing the i-th limit.

Output

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n](0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

Sample test(s)
input
3 11 3 3
output
YES3 3 3
input
3 21 3 31 3 2
output
NO

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=(1<<31)-1;const int maxn=1e5+100;LL col[maxn<<2],sum[maxn<<2];LL l[maxn],r[maxn],q[maxn];int n,m;void pushup(int rs){    sum[rs]=sum[rs<<1]&sum[rs<<1|1];}void pushdown(int rs){    if(col[rs]!=0)    {        col[rs<<1]|=col[rs];        col[rs<<1|1]|=col[rs];        sum[rs<<1]|=col[rs];        sum[rs<<1|1]|=col[rs];        col[rs]=0;    }}void build(int rs,int l,int r){    col[rs]=sum[rs]=0;    if(l==r)   return ;    int mid=(l+r)>>1;    build(rs<<1,l,mid);    build(rs<<1|1,mid+1,r);    pushup(rs);}void update(int rs,int x,int y ,int val,int l,int r){    if(l>=x&&r<=y)    {        col[rs]|=val;        sum[rs]|=val;        return ;    }    pushdown(rs);    int mid=(l+r)>>1;    if(x<=mid)   update(rs<<1,x,y,val,l,mid);    if(y>mid)  update(rs<<1|1,x,y,val,mid+1,r);    pushup(rs);}LL query(int rs,int x,int y,int l,int r){    if(l>=x&&r<=y)   return sum[rs];    pushdown(rs);    int mid=(l+r)>>1;    LL res=INF;    if(x<=mid)   res&=query(rs<<1,x,y,l,mid);    if(y>mid)  res&=query(rs<<1|1,x,y,mid+1,r);    return res;}int main(){    std::ios::sync_with_stdio(false);    while(cin>>n>>m)    {        build(1,1,n);        REP(i,m)        {            cin>>l[i]>>r[i]>>q[i];            update(1,l[i],r[i],q[i],1,n);        }        int flag=1;        REP(i,m)        {            if(query(1,l[i],r[i],1,n)!=q[i])            {                flag=0;                break;            }        }        if(!flag)        {            cout<<"NO"<<endl;            continue;        }        cout<<"YES"<<endl;        REPF(i,1,n)          cout<<query(1,i,i,1,n)<<" ";        cout<<endl;    }    return 0;}

J:CF 475 B  BFS即可。
B. Strongly Connected City
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Imagine a city with n horizontal streets crossing m vertical streets, forming an (n - 1) × (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection.

The mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern.

Input

The first line of input contains two integers n and m, (2 ≤ n, m ≤ 20), denoting the number of horizontal streets and the number of vertical streets.

The second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If the i-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south.

The third line contains a string of length m, made of characters '^' and 'v', denoting direction of each vertical street. If the i-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east.

Output

If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".

Sample test(s)
input
3 3><>v^v
output
NO
input
4 6<><>v^v^v^
output
YES
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )pair<char,char>mp[25][25];typedef pair<int,int>pil;int vis[25][25];char str1[25],str2[25];int n,m;void BFS(int x,int y){    vis[x][y]=1;    pil st,ed;    st.first=x;    st.second=y;    queue<pil>q;    q.push(st);    while(!q.empty())    {        st=q.front();        q.pop();        int xx,yy;        if(mp[st.first][st.second].first=='>')        {            xx=st.first;yy=st.second+1;            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])            {                ed.first=xx;ed.second=yy;                vis[xx][yy]=1;                q.push(ed);            }        }        else if(mp[st.first][st.second].first=='<')        {            xx=st.first;yy=st.second-1;            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])            {                ed.first=xx;ed.second=yy;                vis[xx][yy]=1;                q.push(ed);            }        }        if(mp[st.first][st.second].second=='v')        {            xx=st.first+1;yy=st.second;            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])            {                ed.first=xx;ed.second=yy;                vis[xx][yy]=1;                q.push(ed);            }        }        if(mp[st.first][st.second].second=='^')        {            xx=st.first-1;yy=st.second;            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])            {                ed.first=xx;ed.second=yy;                vis[xx][yy]=1;                q.push(ed);            }        }    }}bool ok(){    int flag=1;    REP(i,n)    {        REP(j,m)        {            if(!vis[i][j])              return false;        }    }    return true;}int main(){    std::ios::sync_with_stdio(false);    while(cin>>n>>m)    {        cin>>str1>>str2;        REP(i,n)        {            REP(j,m)            {                mp[i][j].first=str1[i];                mp[i][j].second=str2[j];            }        }        int flag=1;        REP(i,n)        {            REP(j,m)            {                CLEAR(vis,0);                BFS(i,j);                if(!ok())                {                    flag=0;                    break;                }            }        }        printf("%s\n",flag?"YES":"NO");    }}/*2 2><^v*/






0 0
原创粉丝点击