Harry And Physical Teacher

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 38    Accepted Submission(s): 34

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 speedV0
made a completely elastic collision with a car moving towards the same direction with a speedV(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
 
Source
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  5068 

pid=5067">5067 

pid=5066">5066 5065 5064 

 

题意:

a,b两个球a的速度我va。

b的速度为vb。va>vb.va,vb同向且a在b的后面。

b的质量远大于a。

思路:

a肯定会撞b的。

正规的思路应该是动量守恒。机械能守恒。

然后blabla。我的思路是既然b的质量远大于a。那么b能够看做是一堵墙。而a撞墙的速度就是va-vb咯。因为弹性碰撞。撞墙后速度肯定是反向啦。所以速度就变为-(va-vb)

可是这个速度是相对墙的。所以转化为绝对速度就为-(va-vb)+vb。

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll; int main()
{
int v1,v2; while(~scanf("%d%d",&v1,&v2))
{
printf("%d\n",2*v1-v2);
}
return 0;
}

Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 170    Accepted Submission(s): 50

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.(1≤n,m≤50).

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.( 0≤a[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
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  

pid=5068">5068 

pid=5067">5067 5065 5064 

pid=5062">5062 

 

题意:

哈利珀特学挖掘机了。那么问题来了。。

。。--||。一个n*m(50*50)的格子。一些格子数字大于0个数不超过10(看到这就懂了).你開始在左上角。

然后你要经过全部这些数字大于0的格子然后回到左上角。

然后问你最少须要多少步完毕这个目标。每步能够往四个方向走一步。

思路:

因为数字大于0的格子不超过10所以能够壮压。

这就是经典的TSP问题啦。

先对特殊格子编号然后bfs计算两两特殊格子的距离(步数).然后就TSP了。dp[i][s]表示眼下在i走到的格子状态为s。

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
int maze[55][55],hs[55][55],vis[55][55];
int dp[13][1<<12],dis[15][15],base[15];
int sx[55],sy[55],cnt,n,m;
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
struct node
{
int x,y,d;
};
queue<node> q;
void bfs(int id)
{
while(!q.empty())
q.pop();
node tt,nt;
memset(vis,0,sizeof vis);
tt.x=sx[id],tt.y=sy[id];
dis[id][id]=tt.d=0;
vis[tt.x][tt.y]=1;
q.push(tt);
int nx,ny,i;
while(!q.empty())
{
tt=q.front();
q.pop();
for(i=0;i<4;i++)
{
nx=tt.x+dx[i];
ny=tt.y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny])
{
vis[nx][ny]=1;
nt.x=nx,nt.y=ny,nt.d=tt.d+1;
if(hs[nx][ny]!=-1)
dis[id][hs[nx][ny]]=nt.d;
q.push(nt);
}
}
}
}
int main()
{
int i,j,k,lim; base[0]=1;
for(i=1;i<15;i++)
base[i]=base[i-1]<<1;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
memset(hs,-1,sizeof hs);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&maze[i][j]);
if(maze[i][j]||(i==1&&j==1))
{
sx[cnt]=i,sy[cnt]=j;
hs[i][j]=cnt++;
}
}
}
for(i=0;i<cnt;i++)
bfs(i);
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
lim=1<<cnt;
for(i=0;i<lim;i++)
{
for(j=0;j<cnt;j++)
{
if(dp[j][i]==INF)
continue;
for(k=0;k<cnt;k++)
{
if(i&base[k])
continue;
dp[k][i|base[k]]=min(dp[k][i|base[k]],dp[j][i]+dis[j][k]);
}
}
}
printf("%d\n",dp[0][lim-1]);
}
return 0;
}

Harry And Math Teacher

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 170    Accepted Submission(s): 48

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(2≤n≤50000,1≤m≤50000)
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 (1≤a<b≤n)
follow, indicate the position of Harry and math teacher. Otherwise, there are three integers
x,y,z(1≤x<n,1≤y,z≤2)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
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  

pid=5068" target="_blank">5068 5065 5064 5062 

pid=5061" target="_blank">5061 

 

题意:

有一栋楼。

每层楼有两扇门。

每扇门后面都有两条通向上一层楼的楼梯。楼的层数为n(5e4)。然后m个操作。

0 a b   询问a楼到b楼有多少种走法。

a<b。

1 x y z  改变x层楼y们到x+1层楼z门的楼梯状态(能够走。不能够走)。

思路:

c题赛后。读了下题。发现是大水题。

我们能够用一个2*2的矩阵来表示x层楼到x+1层楼的楼梯状态。1表示能够走。

0表示不能走。然后线段树维护区间乘积即可了。

仅仅是如今的线段树的结点为一个2*2的矩阵罢了。

单点更新区间查询。

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50050;
const int mod=1e9+7;
typedef long long ll;
#define lson L,mid,ls
#define rson mid+1,R,rs
struct Matrix
{
int val[3][3];
Matrix(){ memset(val,0,sizeof val);}
inline Matrix operator *(const Matrix &op)
{
Matrix tt;
int i,j,k;
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
for(k=1;k<=2;k++)
tt.val[i][j]=(tt.val[i][j]+(ll)val[i][k]*op.val[k][j]%mod)%mod;
return tt;
}
} yb[maxn<<2];
void build(int L,int R,int rt)
{
if(L==R)
{
yb[rt].val[1][1]=1;
yb[rt].val[1][2]=1;
yb[rt].val[2][1]=1;
yb[rt].val[2][2]=1;
return;
}
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
build(lson);
build(rson);
yb[rt]=yb[ls]*yb[rs];
}
void update(int L,int R,int rt,int p,int u,int v)
{
if(L==R)
{
yb[rt].val[u][v]^=1;
return;
}
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
if(p<=mid)
update(lson,p,u,v);
else
update(rson,p,u,v);
yb[rt]=yb[ls]*yb[rs];
}
Matrix qu(int L,int R,int rt,int l,int r)
{
if(l<=L&&R<=r)
return yb[rt];
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
Matrix tt;
tt.val[1][1]=tt.val[2][2]=1;
if(l<=mid)
tt=tt*qu(lson,l,r);
if(r>mid)
tt=tt*qu(rson,l,r);
return tt;
}
int main()
{
int n,m,x,y,z,i,op,ans; while(~scanf("%d%d",&n,&m))
{
n--;
build(1,n,1);
for(i=1;i<=m;i++)
{
scanf("%d",&op);
if(!op)
{
scanf("%d%d",&x,&y);
Matrix tt=qu(1,n,1,x,y-1);
ans=0;
for(int j=1;j<=2;j++)
for(int k=1;k<=2;k++)
ans=(ans+tt.val[j][k])%mod;
printf("%d\n",ans);
}
else
{
scanf("%d%d%d",&x,&y,&z);
update(1,n,1,x,y,z);
}
}
}
return 0;
}

Harry And Biological Teacher

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 26    Accepted Submission(s): 2

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 is on his biological class, his teacher is doing experiment with DNA right now. But the clever teacher faces a difficult problem. He has lots of genes. Every time he picks gene a and gene b. If he want to connect gene a and gene b, he should calculate
the length of longest part that the gene a’s suffix and gene b’s prefix can overlap together. For example gene a is "AAT" and gene b is "ATT", then the longest common part is "AT", so the answer is 2. And can you solve this difficult problem for him?
 
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 (1≤n≤100000,1≤m≤100000)on
the first line, indicate the number of genes and the number of queries.

The next following n line, each line contains a non-empty string composed by characters 'A' , 'T' , 'C' , 'G'. The sum of all the characters is less than 100000.

Then the next following m line, each line contains two integers a and b(1≤a,b≤n),
indicate the query.
 
Output
For each query, you should output one line that contains an integer indicate the length of longest part that the gene a’s suffix and gene b’s prefix can overlap together.
 
Sample Input
2 1
ACCGT
TTT
1 2
4 4
AAATTT
TTTCCC
CCCGGG
GGGAAA
1 2
2 3
3 4
4 1
 
Sample Output
1
3
3
3
3
 
Source
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  

pid=5068">5068 5067 5065 5064 

pid=5062">5062 

 

题意:

给你n个由A,G,C,T构成的字符串。n个字符串的总长度不超过1e5.然后m个询问。

n。m<=1e5.每次询问a,b两个字符串。找到一个最长的串使得它是a的后缀。然后也是b的前缀。输出它的长度即可了。

思路:

第一想法就是后缀自己主动机。然后就开搞了。离线处理全部询问。把全部第一个串是a的b建个邻接表。然后依次处理每一个a。先对a建后缀自己主动机。然后对于串b在自己主动机上匹配就是了。匹配的最大长度的可接受态就是答案。然后一A。甚欢。比赛结束后才发现忘了记忆化。就是对每一个a链里的每一个b仅仅须要跑一遍即可了。哎。可惜发现的太晚了。还是无情的T了。

就上后再就200ms过了。。

。真是冤死了。时间复杂度。因为顶多对n各字符串都建一次后缀自己主动机。然后每次询问最多也仅仅能匹配a串那么长。

所以复杂度为2*总串长。

就O(n)咯。

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
char txt[maxn<<1];
int st[maxn],ans[maxn];
map<int,int> mp;
struct node
{
node *f,*ch[30];
int len,ac;
void init()
{
ac=0;
memset(ch,0,sizeof ch);
}
}*root,*tail,sam[maxn<<1];
int tot,len,cnt;
struct qnode
{
qnode *next;
int v,id;
} ed[maxn<<1],*head[maxn];
void adde(int u,int v,int id)
{
ed[cnt].v=v;
ed[cnt].id=id;
ed[cnt].next=head[u];
head[u]=&ed[cnt++];
}
void init()
{
tot=0;
root=tail=&sam[tot++];
root->init();
root->len=0;
}
void Insert(int c)
{
node *p=tail,*np=&sam[tot++];
np->init(),np->len=p->len+1;
while(p&&!p->ch[c])
p->ch[c]=np,p=p->f;
tail=np;
if(!p)
np->f=root;
else
{
if(p->ch[c]->len==p->len+1)
np->f=p->ch[c];
else
{
node *q=p->ch[c],*nq=&sam[tot++];
*nq=*q;
nq->len=p->len+1;
q->f=np->f=nq;
while(p&&p->ch[c]==q)
p->ch[c]=nq,p=p->f;
}
}
}
int main()
{
int i,j,n,m,len,tl,u,v; while(~scanf("%d%d",&n,&m))
{
len=cnt=0;
for(i=1;i<=n;i++)
{
st[i]=len;
scanf("%s",txt+len);
tl=strlen(txt+len);
len=len+tl+1;
}
memset(head,0,sizeof head);
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
adde(u,v,i);
}
for(i=1;i<=n;i++)
{
init();
for(j=st[i];txt[j];j++)
Insert(txt[j]-'A');
for(node *p=tail;p!=NULL;p=p->f)
p->ac=1;
mp.clear();
for(qnode *q=head[i];q!=NULL;q=q->next)
{
if(mp.count(q->v))
{
ans[q->id]=mp[q->v];
continue;
}
node *p=root;
int ct=0,aa=0;
for(j=st[q->v];txt[j];j++)
if(p->ch[txt[j]-'A']!=NULL)
{
ct++,p=p->ch[txt[j]-'A'];
if(p->ac)
aa=max(aa,ct);
}
else
break;
ans[q->id]=aa;
mp[q->v]=aa;
}
}
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

BestCoder Round #14的更多相关文章

  1. hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)

    Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】

    称号:Harry And Dig Machine 哈哈  最终涨边粉色了,不easy呀.顺便写一道题解吧 题意:给一个m*n的矩阵,然后当中最多由10个有值,求总左上角把全部的值都拿上回到左上角的最小 ...

  3. BestCoder Round #11 (Div. 2) 题解

    HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. 简单几何(水)BestCoder Round #50 (div.2) 1002 Run

    题目传送门 /* 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 */ /***** ...

  5. HDU 5228 ZCC loves straight flush( BestCoder Round #41)

    题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...

  6. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

  7. BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元

    BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy  Init函数 然后统计就ok B. 博弈 题  不懂  推了半天的SG.....  结果这 ...

  8. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  9. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. C语言的本质(13)——指向指针的指针

    指针可以指向基本类型,也可以指向复合类型,因此一个指针变量存放的可以是另一个指针变量的地址,则称这个指针变量为指向指针的指针变量.由于指针变量直接指向变量,所以称为"单级间址".而 ...

  2. Eclipse 安装mybatis的编辑插件

    1.MyEditor安装的方式 Eclipse 安装mybatis的编辑插件有以下4种方式,您可以使用下列方法之一来安装MyBatis的编辑器: Eclipse 3.7的(市场客户机安装):此图像拖放 ...

  3. win7 Visual Studio 2008 安装程序时出现“ 加载安装组件时遇到问题。取消安装。”处理方法

    win7 Visual Studio 2008 安装程序时出现“ 加载安装组件时遇到问题.取消安装.”处理方法 vs2008试用期到期,卸载.重新安装都会出现“ 加载安装组件时遇到问题.取消安装.”无 ...

  4. POJ3307+找规律

    /* 题意:求第N个productivity property数是谁. (productivity property数:就是这个数可以由另外的数的各个位上的乘积得到.) */ #include< ...

  5. 我使用过的Linux命令之file - 检测并显示文件类型

    摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...

  6. Java中的import

    有些人写了一阵子 Java,可是对于 Java的 package 跟 import 还是不太了解.很多人以为原始码 .java 文件中的 import 会让编译器把所 import 的程序通通写到编译 ...

  7. [AS3]as3用ByteArray来对SWF文件编码加密实例参考

    [AS3]as3用ByteArray来对SWF文件编码加密实例参考,简单来说,就是将 swf 以 binary 的方式读入,并对 ByteArray 做些改变,再重新存成 swf 档.这个作业当然也可 ...

  8. 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?

    做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...

  9. poj3461Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  10. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...