A. Sagheer and Crossroads
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sagheer is walking in the street when he comes to an intersection of two roads. Each road can be represented as two parts where each part has 3 lanes getting into the intersection (one for each direction) and 3 lanes getting out of the intersection, so we have 4 parts in total. Each part has 4 lights, one for each lane getting into the intersection (l — left, s — straight, r — right) and a light p for a pedestrian crossing.

An accident is possible if a car can hit a pedestrian. This can happen if the light of a pedestrian crossing of some part and the light of a lane that can get to or from that same part are green at the same time.

Now, Sagheer is monitoring the configuration of the traffic lights. Your task is to help him detect whether an accident is possible.

Input

The input consists of four lines with each line describing a road part given in a counter-clockwise order.

Each line contains four integers l, s, r, p — for the left, straight, right and pedestrian lights, respectively. The possible values are 0 for red light and 1 for green light.

Output

On a single line, print "YES" if an accident is possible, and "NO" otherwise.

Examples
Input
1 0 0 1
0 1 0 0
0 0 1 0
0 0 0 1
Output
YES
Input
0 1 1 0
1 0 1 0
1 1 0 0
0 0 0 1
Output
NO
Input
1 0 0 0
0 0 0 1
0 0 0 0
1 0 1 0
Output
NO
Note

In the first example, some accidents are possible because cars of part 1 can hit pedestrians of parts 1 and 4. Also, cars of parts 2 and 3 can hit pedestrians of part 4.

In the second example, no car can pass the pedestrian crossing of part 4 which is the only green pedestrian light. So, no accident can occur.

题意:逆时针给你各个路口以及人行道信号灯的情况 判断是否可能发生车祸(车撞人)

题解:判断人行道点绿灯的路口是否会有车开过来

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll __int64
int a[][];
int main()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
scanf("%d",&a[i][j]);
}
for(int i=;i<;i++)
{
if(a[i][]==)
{
for(int j=;j<;j++)
{
if(a[i][j]==)
{
printf("YES\n");
return ;
}
}
if(a[(i+)%][]==||a[(i+)%][]==||a[(i+)%][]==)
{
printf("YES\n");
return ;
}
}
}
printf("NO\n");
return ;
}
B. Sagheer, the Hausmeister
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output

Print a single integer — the minimum total time needed to turn off all the lights.

Examples
Input
2 2
0010
0100
Output
5
Input
3 4
001000
000010
000010
Output
12
Input
4 3
01110
01110
01110
01110
Output
18
Note

In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

题意:给你n*(m+2)的01矩阵 第一列与最后一列代表左右两个楼梯(都为0)  现在初始位置在(n,1)  目的是将所有的1变为0  走一步耗时1 改变数字不耗时 只有当前行都变为0才能通过楼梯向上走(耗时1) 问最少的耗时使得所有的1变为0

题解:因为n只有15  所以可以枚举所有的走楼梯的情况(2^n种) (注意若当前行的上方已经没有1了 则不需要继续往上走 也不需要再回到楼梯所在的位置)

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll __int64
int n,m;
char a[][];
int main()
{
scanf("%d %d",&n,&m);
for(int i=; i<n; i++)
scanf("%s",a[i]);
int exm=<<n;
int bit[];
int maxn=;
int jishu=;
int flag;
int zha=n-;
for(int i=;i<n-;i++)
{
flag=;
for(int k=;k<m+;k++)
{
if(a[i][k]==''&&flag==)
{
flag=;
jishu=n--i;
zha=i;
}
}
if(flag==)
break;
}
for(int i=; i<exm; i++)
{
int len=;
int s=i;
while(s>)
{
bit[len++]=s%;
s/=;
}
for(int j=len; j<n; j++)
bit[j]=;
int now=;
int ans=jishu;
int ji;
for(int j=n-; j>zha; j--)
{
if(now==)
{
ji=;
if(bit[j]==)
{
for(int k=; k<m+; k++)
{
if(a[j][k]=='')
ji=k;
}
ans+=(ji*);
now=bit[j];
}
else
{
ans+=(m+);
now=bit[j];
}
}
else
{
ji=m+;
if(bit[j]==)
{
ans+=(m+);
now=bit[j];
}
else
{
for(int k=m; k>=; k--)
{
if(a[j][k]=='')
ji=k;
}
ans+=(m+-ji)*;
now=bit[j];
}
}
}
if(now==)
{
ji=;
for(int k=; k<m+; k++)
{
if(a[zha][k]=='')
ji=k;
}
ans+=ji;
}
else
{
ji=m+;
for(int k=m; k>=; k--)
{
if(a[zha][k]=='')
ji=k;
}
ans+=(m+-ji);
}
maxn=min(ans,maxn);
}
printf("%d\n",maxn);
return ;
}
C. Sagheer and Nubian Market
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buys k items with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

Input

The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs.

Output

On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.

Examples
Input
3 11
2 3 5
Output
2 11
Input
4 100
1 2 5 6
Output
4 54
Input
1 7
7
Output
0 0
Note

In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5, 10, 17, 22].

In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.

题意:给你n个物品 基础价格为ai   价格为axj + xj·k  拥有的钱数为S  问最多能买多少件物品 以及所需要的最少钱数

题解:二分k  注意LL

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#define ll __int64
using namespace std;
ll n,s;
ll a[];
ll b[];
bool fun(ll k)
{
for(int i=;i<=n;i++)
b[i]=a[i]+i*k;
sort(b+,b++n);
ll sum=;
for(int i=;i<=k;i++)
sum+=b[i];
if(sum<=s)
return true;
else
return false; }
int main ()
{
scanf("%I64d %I64d",&n,&s);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
ll l=,r=n,mid;
ll ans;
while(l<=r)
{
mid=(l+r)/;
if(fun(mid)){
l=mid+;
ans=mid;
}
else
r=mid-;
}
cout<<ans<<" ";
for(int i=;i<=n;i++)
b[i]=a[i]+i*ans;
sort(b+,b++n);
ll sum=;
for(int i=;i<=ans;i++)
sum+=b[i];
cout<<sum<<endl;
return ;
}
E. Sagheer and Apple Tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered from 1 to n and rooted at node 1. The i-th node has ai apples. This tree has a special property: the lengths of all paths from the root to any leaf have the same parity (i.e. all paths have even length or all paths have odd length).

Sagheer and Soliman will take turns to play. Soliman will make the first move. The player who can't make a move loses.

In each move, the current player will pick a single node, take a non-empty subset of apples from it and do one of the following two things:

  1. eat the apples, if the node is a leaf.
  2. move the apples to one of the children, if the node is non-leaf.

Before Soliman comes to start playing, Sagheer will make exactly one change to the tree. He will pick two different nodes u and v and swap the apples of u with the apples of v.

Can you help Sagheer count the number of ways to make the swap (i.e. to choose u and v) after which he will win the game if both players play optimally? (u, v) and (v, u) are considered to be the same pair.

Input

The first line will contain one integer n (2 ≤ n ≤ 105) — the number of nodes in the apple tree.

The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the number of apples on each node of the tree.

The third line will contain n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n) — the parent of each node of the tree. Node i has parent pi (for 2 ≤ i ≤ n). Node 1 is the root of the tree.

It is guaranteed that the input describes a valid tree, and the lengths of all paths from the root to any leaf will have the same parity.

Output

On a single line, print the number of different pairs of nodes (u, v), u ≠ v such that if they start playing after swapping the apples of both nodes, Sagheer will win the game. (u, v) and (v, u) are considered to be the same pair.

Examples
Input
3
2 2 3
1 1
Output
1
Input
3
1 2 3
1 1
Output
0
Input
8
7 2 2 5 4 3 1 1
1 1 1 4 4 5 6
Output
4
Note

In the first sample, Sagheer can only win if he swapped node 1 with node 3. In this case, both leaves will have 2 apples. If Soliman makes a move in a leaf node, Sagheer can make the same move in the other leaf. If Soliman moved some apples from a root to a leaf, Sagheer will eat those moved apples. Eventually, Soliman will not find a move.

In the second sample, There is no swap that will make Sagheer win the game.

Note that Sagheer must make the swap even if he can win with the initial tree.

题意:给你一颗树(根到叶子的长度都为奇数或都为偶数)  以及点权值 两个人做游戏  两种操作 1.若点为叶子结点 则使得点权减少1  2.若点不为叶子结点 则取出一点权值加到它的任意一个子节点上   若轮到某人无法操作则输

问有多少种方法 只交换两个点的点权 使得先手获胜

题解:阶梯博弈转化为nim博弈 然后考虑一下如何组合出答案。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#define ll __int64
using namespace std;
ll n;
struct node
{
ll pre;
ll to;
} N[];
ll a[];
ll nedge=;
ll pre[];
ll used[];
ll ji[],ji1;
ll ou[],ou1;
map<ll,ll>nm1;
map<ll,ll>nm2;
ll zong;
void add(ll s,ll t)
{
nedge++;
N[nedge].to=t;
N[nedge].pre=pre[s];
pre[s]=nedge;
}
void dfs(ll now,ll cen)
{
used[now]=;
if(cen%==)
{
ou[ou1++]=a[now];
nm1[a[now]]++;
}
else
{
ji[ji1++]=a[now];
nm2[a[now]]++;
}
for(int i=pre[now]; i; i=N[i].pre)
{
if(used[N[i].to]==)
{
dfs(N[i].to,cen+);
}
}
zong=max(cen,zong);
}
int main()
{
scanf("%I64d",&n);
ji1=;
ou1=;
zong=;
for(int i=; i<=n; i++)
scanf("%I64d",&a[i]);
ll exm;
for(int i=; i<n; i++)
{
scanf("%I64d",&exm);
add(i+,exm);
add(exm,i+);
}
dfs(,);
ll sum1=,sum2=;
for(int i=; i<ji1; i++)
sum1^=ji[i];
for(int i=; i<ou1; i++)
sum2^=ou[i];
ll ans=;
if(zong%==)
{
if(sum1==)
ans=ans+(ll)ji1*(ji1-)/+(ll)ou1*(ou1-)/;
for(int i=; i<ji1; i++)
{
int exm=ji[i]^sum1;
ans=ans+nm1[exm];
}
}
else
{
if(sum2==)
ans=ans+ji1*(ji1-)/+ou1*(ou1-)/;
for(int i=; i<ou1; i++)
{
int exm=ou[i]^sum2;
ans=ans+nm2[exm];
}
}
printf("%I64d\n",ans);
return ;
}

Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈的更多相关文章

  1. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  2. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  3. Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)

    http://codeforces.com/contest/812/problem/D 题意: 现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果 ...

  4. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...

  5. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP

    题目链接:http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test ...

  6. [Codeforces Round#417 Div.2]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交  D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...

  7. Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad

    [题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...

  8. Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

随机推荐

  1. 存储过程关于LOOP循环问题

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 10 月 17 日 原地址:https://niaobulashi.com/archives/procedures_loop. ...

  2. mysql数据导到本地

    需求: 把mysql查询结果导出到txt(其他格式亦可),放在本地,供下一步使用 首先网上查了下,select * from driver into outfile 'a.txt'; 前面是你的sql ...

  3. Hbase 教程-安装

    HBase安装 安装前设置 安装Hadoop在Linux环境下之前,需要建立和使用Linux SSH(安全Shell).按照下面设立Linux环境提供的步骤. 创建一个用户 首先,建议从Unix创建一 ...

  4. Python中如何实现im2col和col2im函数(sliding类型)

    今天来说说im2col和col2im函数,这是MATLAB中两个内置函数,经常用于数字图像处理中.其中im2col函数在<MATLAB中的im2col函数>一文中已经进行了简单的介绍. 一 ...

  5. cinder创建volume的流程-简单梳理

    1. cinder-api接收到创建的请求,入口:cinder.api.v2.volumes.VolumeController#create,该方法主要负责一些参数的重新封装和校验,然后调用cinde ...

  6. OA系统与Exchange 日历打通

    目前我碰到好几个案例是希望将客户以后的OA系统与Exchange中的日历系统相结合,比如致远或者泛微的OA系统. 客户的需求如下: 1.有了OA系统 2.客户使用Outlook当邮件客户端 3.客户希 ...

  7. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

  8. 团队backlog和燃尽图

    首先我们的团队的任务是做一款一对一交流的软件,我们团队的backlog如下: 用电脑搭建一个服务器一对一平台 实现数据库和Android的链接 编写登陆,注册页面,和登陆成功界面,和登陆失败页面 实现 ...

  9. java锁有哪些类(转)

    转载来源:http://www.cnblogs.com/lxmyhappy/p/7380073.html 1.Java都有哪些锁? 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁/读写锁 乐观锁/ ...

  10. CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)

    http://codeforces.com/contest/1092/problem/D2 Vova's family is building the Great Vova Wall (named b ...