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. python-map, reduce, filter, lambda

    目录 lambda表达式 reduce()函数 map()函数 filter()函数 tips:以下使用到的迭代器,可迭代对象,生成器等概念可以参见我的另一篇博客 lambda表达式 主要用于一行写完 ...

  2. win2003系统网络安装——基于linux+pxe+dhcp+tftp+samba+ris

    原文发表于:2010-09-16 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...

  3. 如何使用phpredis连接Redis的方法

    本文跟大家介绍使用同一VPC内弹性云服务器ECS上的phpredis连接Redis的方法. 更多的客户端的使用方法,请参考https://redis.io/clients 前提条件 已成功申请Redi ...

  4. python3【基础】-list&tuple

    一.list概述 list (列表)是python中最常用的数据类型之一,通过列表可以对数据实现最方便的存储,修改等操作.在python3中,list支持如下方法: Help on class lis ...

  5. Fiber Network ZOJ 1967(Floyd+二进制状态压缩)

    Description Several startup companies have decided to build a better Internet, called the "Fibe ...

  6. Alpha发布—文案+美工展示

    目录 团队简介 项目进展 组内分工 队员总结 后期计划 一.团队简介 二.项目进展 从选题发布到今天的Alpha发布,我们团队经历了许许多多的磨难.我们最终设计了如下的功能:首页.班级.个人.更多.打 ...

  7. MySql 8 命令

    1-创建用户 create user 用户名@'%' identified by '密码'; create user 用户名@'localhost' identified by '密码';   2-授 ...

  8. asp.net如何实现跟踪检查用户知否查看了邮件。

    有时我们有这样一种需求场景,我们给很多用户发了邮件,需要一个反馈,用户是否查看了我们发送的邮件,百度了以下果然有方案. 我总结实践了下这个过程,同时有自己的一点使用感受.记录下希望对你有帮助. 有人想 ...

  9. VC++中使用用户自定义消息及自定制窗口技巧

    Windows 应用程序所要做的每项工作几乎都是基于消息处理的, Windows 系统消息分为常用 Windows 消息,控件通知消息和命令.然而,有时我们需要定义自己的消息来通知程序什么事情发生了, ...

  10. Swift-自定义类的构造函数

    构造函数类似oc中的init方法默认情况下,创建一个,类会调用一个构造函数即使没写任何构造函数,编译器会默认一个构造函数如果是继承NSObject,可以对构造函数重写 class Person: NS ...