您的位置:首页 > 其它

Codeforces 294比赛记

2017-06-13 18:01 176 查看
Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass’s territory. Supposed there are ai oskols sitting on the i-th wire.

Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away.

Shaass has shot m birds. You’re given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

Input

The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, …, an, (0 ≤ ai ≤ 100).

The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It’s guaranteed there will be at least yi birds on the xi-th wire at that moment.

Output

On the i-th line of the output print the number of birds on the i-th wire.

Example

Input

5

10 10 10 10 10

5

2 5

3 13

2 12

1 13

4 6

Output

0

12

5

0

16

Input

3

2 4 1

1

2 2

Output

3

0

3

拿到试题先看第一题,水题,果断切。就是强行模拟,1Y

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
ll a[101];
int m,x,y,n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) cin>>a[i];
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
a[x-1]+=y-1;
a[x+1]+=(a[x]-y);
a[x]=0;
}
for(int i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
}


再看第二题,看了一下题目,感觉是搜索,打了几发发现好像可以二分答案+贪心验证,贪心验证比较好想,然后后来发现二分不对,因为余1,余0得分开考虑,看顺序时间复杂度够,就果断顺序扫,然后就过了

Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf’s dimensions to be as small as possible. The thickness of the i-th book is ti and its pages’ width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

Input

The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

Example

Input

5

1 12

1 3

2 15

2 5

2 1

Output

5

Input

3

1 10

2 1

2 4

Output

3

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 101
int ans;
int n;
int k2
;
int l1,l2;
int k1
;
int l,r=0;
int ho,ku;
bool cmp(int x,int y)
{
return x>y;
}
bool pd(int x)
{
int res=0;
for(int i=0;i<=x/2;i++)
{
if(i>l2) break;
res=0;
int j=x-2*i;
if(j>l1) continue;
if(j<0) break;
for(int k=i+1;k<=l2;k++) res+=k2[k];
for(int k=j+1;k<=l1;k++) res+=k1[k];
if(res<=x) return 1;
}
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&ho,&ku);
if(ho==1) k1[++l1]=ku;
else k2[++l2]=ku;
r+=ho;
}
l=1;
sort(k1+1,k1+l1+1,cmp);
sort(k2+1,k2+l2+1,cmp);
//  for(int i=1;i<=l2;i++) cout<<k2[i]<<" ";
//  cout<<r<<endl;
ans=r;
for(int i=l;i<=r;i++)
{
if(pd(i))
{
ans=min(ans,i);
}
}
printf("%d",ans);
}


打完才36min感觉C题一开始看不可做,但是毕竟题目都是有解的,我考虑了一下补集转化,然后数
4000
学推了一下,跳了半天才过,过完只剩20min没时间打其他题了,就不再打下去

技巧:考试时极限情况考虑清楚,一定不能慌,求稳

一看排名:RANK 2,还好,主要是我正确率高,不是1Y就是2Y。

然后下午就来刚T4和T5

先刚T5,发现是一个求树的重心的问题(以重心为根的树,最大的儿子size最小:最大的儿子的sz小于一半的所有孩子的sz,单独考虑那条边推一发)

然后就是两遍dfs+容斥求每个点到所有子树中其他点的距离,具体看代码,挺巧妙,然后就是每个点被算了两遍,加的时候加上2*sum最后ans除以2就可以了。这是一道树形DP和树上求值的应用

Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of n·m square tiles forming a n × m rectangle. Therefore he’s decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.

Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (xs, ys) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it’s painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn’t lead to any painting. The first tile the robot is standing on, is also painted.

The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.

Let’s consider an examples depicted below.

If the robot starts at tile number 1 (the tile (1, 1)) of the left grid heading to down-right it’ll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3.

Input

The first line of the input contains two integers n and m, (2 ≤ n, m ≤ 105). The second line contains two integers xs and ys (1 ≤ xs ≤ n, 1 ≤ ys ≤ m) and the direction robot is facing initially. Direction is one of the strings: “UL” (upper-left direction), “UR” (upper-right), “DL” (down-left) or “DR” (down-right).

Note, that record (xs, ys) denotes the tile that is located at the xs-th row from the top and at the ys-th column from the left of the kitchen.

It’s guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles).

Output

Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Example

Input

3 4

1 1 DR

Output

7

Input

3 4

3 3 DR

Output

11

Input

3 3

1 1 DR

Output

-1

Input

3 3

1 2 DL

Output

4

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define forw(i,x) for(int i=fir[x];i;i=ne[i])
#define M 20001
#define N M
LL C[M];
int cnt=1,ne[M],fir[M],to[M],from[M];
int n;
LL f
;
int S
;
LL ans =1e18;
int x,y;
LL z;
void dfs(int x,int fa)
{
f[x]=0;S[x]=1;
forw(i,x)
{
int V=to[i];
if(V!=fa)
{
dfs(V,x);
S[x]+=S[V];
f[x]+=f[V]+C[i]*S[V];
}
}
}
void add(int x,int y,LL z)
{
to[++cnt]=y;C[cnt]=z;ne[cnt]=fir[x];fir[x]=cnt;from[cnt]=x;
}
void DFS(int x,int fa,LL &p,int sum)
{
p=min(p,f[x]);
forw(i,x)
{
int V=to[i];
if(V!=fa)
{
f[to[i]]=f[x]+C[i]*(sum-S[V]*2);
DFS(V,x,p,sum);
}
}
}
int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
for(int i=2;i<=cnt;i+=2)
{
int U=from[i];int V=to[i];
dfs(U,V);
dfs(V,U);
LL p1=1e18,p2=1e18;
DFS(U,V,p1,S[U]);DFS(V,U,p2,S[V]);
long long dance=0;
for(int j=1;j<=n;j++) dance+=f[j];
long long it;
it=dance+2*(S[U]*S[V]*C[i]+p1*S[V]+p2*S[U]);
ans=min(ans,it);
}
cout<<ans/2;
return 0;
}




然后看D题一道大模拟,然后给我的其实就是:模拟的时候可以考虑边界情况,会让复杂的模拟变简单!这里就是考虑它触边(n+m-2)次时就行了,搞了2*(n+m-2)次还不行,就说明无解

Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of n·m square tiles forming a n × m rectangle. Therefore he’s decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.

Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (xs, ys) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it’s painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn’t lead to any painting. The first tile the robot is standing on, is also painted.

The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.

Let’s consider an examples depicted below.

If the robot starts at tile number 1 (the tile (1, 1)) of the left grid heading to down-right it’ll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3.

Input

The first line of the input contains two integers n and m, (2 ≤ n, m ≤ 105). The second line contains two integers xs and ys (1 ≤ xs ≤ n, 1 ≤ ys ≤ m) and the direction robot is facing initially. Direction is one of the strings: “UL” (upper-left direction), “UR” (upper-right), “DL” (down-left) or “DR” (down-right).

Note, that record (xs, ys) denotes the tile that is located at the xs-th row from the top and at the ys-th column from the left of the kitchen.

It’s guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles).

Output

Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Example

Input

3 4

1 1 DR

Output

7

Input

3 4

3 3 DR

Output

11

Input

3 3

1 1 DR

Output

-1

Input

3 3

1 2 DL

Output

4

//神模拟
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<new>
using namespace std;
bool w[4][1000001];
long long ans;
int cnt,tot;
int x,y;
int dx,dy;
string ty;
int n,m;
bool flag=0;
void doit(int x,int y)
{
int a,b;
if(x==1) a=0,b=y;else if(x==n) a=1,b=y;else if(y==1) a=2,b=x;else a=3,b=x;
if(!w[a][b])w[a][b]=1,tot++;
if(x==1) dx=1;if(x==n) dx=-1;if(y==1) dy=1;if(y==m) dy=-1;
}
int main()
{
cin>>n>>m;
cin>>x>>y>>ty;
if(ty[0]=='U')dx=-1;else dx=1;
if(ty[1]=='L')dy=-1;else dy=1;
ans=1;tot=1;
for(int i=1;i<=2*(n+m-2);i++)
{
doit(x,y);
if(flag){puts("-1");return 0;}
if(tot>=n+m-1) {cout<<ans<<endl;return 0;}
int ww=min(dx>0?n-x:x-1,dy>0?m-y:y-1);
x+=dx*ww;y+=dy*ww;ans+=ww;
}
puts("-1");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces