BC14hdoj5066&&hdoj5067&&hdoj5068

105 篇文章 0 订阅
18 篇文章 0 订阅

Harry And Physical Teacher

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


Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm. 
Today, Harry's physical teacher set him a difficult problem: if a small ball moving with a speed  V0  made a completely elastic collision with a car moving towards the same direction with a speed  V(V<V0) , and the car far outweighs the ball, what was the speed of the small ball after the collision?
This problem was so difficult that Harry hasn't figure out how to solve it. Therefore, he asks you for help. Could you do him this favor?
 

Input
They are several test cases, you should process to the end of file.
There are two integers  V  and  V0  for each test case. All the integers are 32-bit signed non-negative integers.
 

Output
For each test case, just output one line that contains an integer indicate the speed of the small ball after the collision.
 

Sample Input
  
  
0 10
 

Sample Output
  
  
-10
 

物理题:按照我的想法因为小球质量远远小于车的质量所以当小球碰到车后即按原速度反弹,小球相对小车的速度为v0-v;所以反弹的速度为v-v0因为车有与反弹方向相反的速度所以速度即为v+v-v0

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=10010;
int main()
{
    int t;
    long long v0,v1;
    while(scanf("%lld%lld",&v1,&v0)!=EOF){
        printf("%lld\n",2*v1-v0);
    }
    return 0;
}

Harry And Dig Machine

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


Problem Description
  As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm. 
  Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
 

Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m. (1n,m50) .
The next n line, each line contains m integer. The j-th number of  ith  line a[i][j] means there are a[i][j] stones on the  jth  cell of the  ith  line.(  0a[i][j]100  , and no more than 10 of a[i][j] will be positive integer).
 

Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
 

Sample Input
  
  
3 3 0 0 0 0 100 0 0 0 0 2 2 1 1 1 1
 

Sample Output
  
  
4 4
 

Source
 

由于Harry的dig machine是无限大的,而装载石头和卸载石头是不费时间的,所以问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少。这就是经典的旅行商问题,考虑到我们必须要遍历的点只有不到10个,可以用状态压缩解决。  Dp[i][j]Dp[i][j]Dp[i][j] 表示i状态的点被访问过了,当前停留在点j 需要的最少时间。枚举另一点不在i状态内的点k,从点j节点走向点k,状态转移  Dp[i∣(1≪k)][k]=min(Dp[i∣(1≪k)][k],Dp[i][j]+Dis(j,k))Dp[i|(1 \ll k)][k] = min ( Dp[i|(1 \ll k)][k] , Dp[i][j] + Dis(j,k) )Dp[i(1k)][k]=min(Dp[i(1k)][k],Dp[i][j]+Dis(j,k))  其中 Dis(j,k)Dis ( j , k )Dis(j,k) 表示点j与点k的最短距离,这个可以通过坐标O(1)计算得到。若有t个点包含石头,则算法复杂度为 O(n∗m+(t2)∗(2t))O(n*m+(t^2) * (2^t))O(nm+(t2)(2t))

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2510;
struct Node{
    int x,y;
}A[maxn];
int cost[11][11];
int dp[1<<11][maxn];
int main()
{
    int n,m,i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF){
        int cnt=0,num;
        for(i=1;i<=n;++i){
            for(j=1;j<=m;++j){
                scanf("%d",&num);
                if(num>0){
                    A[cnt].x=i;A[cnt].y=j;cnt++;
                }
            }
        }
        if(cnt==0){
            printf("0\n");
            continue;
        }
        for(i=0;i<(1<<cnt);++i){
            for(j=0;j<cnt;++j){
                dp[i][j]=inf;
            }
        }
        for(i=0;i<cnt;++i){
            for(j=0;j<cnt;++j){
                cost[i][j]=abs(A[i].x-A[j].x)+abs(A[i].y-A[j].y);
            }
        }
        for(i=0;i<cnt;++i){
            dp[1<<i][i]=A[i].x+A[i].y-2;
        }
        for(i=0;i<(1<<cnt);++i){
            for(j=0;j<cnt;++j){
                if(dp[i][j]==inf)continue;
                for(k=0;k<cnt;++k){
                    if(i&(1<<k))continue;
                    dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+cost[j][k]);
                }
            }
        }
        int ans=inf;
        for(i=0;i<cnt;++i){
            ans=min(ans,dp[(1<<cnt)-1][i]+A[i].x+A[i].y-2);
        }
        printf("%d\n",ans);
    }
    return 0;
} 

Harry And Math Teacher

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 506    Accepted Submission(s): 155


Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm. 
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
 

Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and  m(2n50000,1m50000)  in the first line, indicate the number of the castle’s layers and the number of queries. And the following m lines, each line contains three or four integers. If the first integer op equals 0, there are two integers a and b ( 1a<bn ) follow, indicate the position of Harry and math teacher. Otherwise, there are three integers  x,y,z(1x<n,1y,z2) follow, it means that the stair between the  xth  floor’s  yth  door and the  (x+1)th  floor’s z-th door changes its state(if it is intact, then it breaks else it joints).
 

Output
For each query, if op equals 0, you should output one line that contains an integer indicates the number of ways from  ath  floor to the  bth  floor.
 

Sample Input
  
  
3 1 0 1 3 3 2 1 2 1 1 0 1 3
 

Sample Output
  
  
8 6
 

Source
 

我们可以把第i层跟第i+1层之间楼梯的通断性构造成一个2*2的通断性矩阵,1表示通,0表示不通。那么从第a层到第b层,就是将a到b-1的通断性矩阵连乘起来,然后将得到的答案矩阵上的每个元素加起来即为方案数。想到矩阵的乘法是满足结合律的,那么我们可以用线段树来维护矩阵的乘积。每次我们只会修改某一个楼梯的通断性,所以就只是简单的线段树单点更新,成段求乘积而已。 整体复杂度 2∗2∗2∗nlogn2*2*2*nlogn222nlogn

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MOD 1000000007
using namespace std;
const int maxn=50010;
struct Node{
    long long f[2][2];
}tree[maxn<<2];
void pushup(Node A,Node B,Node &C){
    memset(C.f,0,sizeof(C.f));
    for(int i=0;i<2;++i){
        for(int k=0;k<2;++k){
            if(A.f[i][k]){
                for(int j=0;j<2;++j){
                    C.f[i][j]=(C.f[i][j]+A.f[i][k]*B.f[k][j])%MOD;
                }
            }
        }
    }
}
void build(int l,int r,int rt){
    if(l==r){
        for(int i=0;i<2;++i){
            for(int j=0;j<2;++j){
                tree[rt].f[i][j]=1;
            }
        }
        return ;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(tree[rt<<1],tree[rt<<1|1],tree[rt]);
}
void update(int x,int y,int z,int l,int r,int rt){
    if(l==r){
        tree[rt].f[y][z]^=1;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid)update(x,y,z,lson);
    else update(x,y,z,rson);
    pushup(tree[rt<<1],tree[rt<<1|1],tree[rt]);
}
Node query(int left,int right,int l,int r,int rt){
    if(left==l&&r==right){
        return tree[rt];
    }
    int mid=(l+r)>>1;
    if(right<=mid)return query(left,right,lson);
    if(left>mid)return query(left,right,rson);
    Node ans;
    pushup(query(left,mid,lson),query(mid+1,right,rson),ans);
    return ans;
}
int main()
{
    int n,m,i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF){
        build(1,n-1,1);
        while(m--){
            int oper;
            scanf("%d",&oper);
            if(oper==0){
                int a,b;
                scanf("%d%d",&a,&b);
                Node ans=query(a,b-1,1,n-1,1);
                long long cnt=(ans.f[0][0]+ans.f[0][1]+ans.f[1][0]+ans.f[1][1])%MOD;
                printf("%lld\n",cnt);
            }
            else {
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                y--;z--;
                update(x,y,z,1,n-1,1);
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值