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. 教你thinkphp5怎么配置二级域名

    有些项目要将移动端和PC端分离开来,比如访问xxx.com,展示的是PC端的页面.而访问m.xxx.com,展示的是移动端的页面.thinkphp源码需要多多学习,这里记录一下知识点,顺便分享给需要的 ...

  2. hive 2以上版本启动异常 Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

    hive2.0以上的版本启动时 抛出 “Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreCli ...

  3. CSS布局之圣杯布局和双飞翼布局

    其实圣杯布局和双飞翼布局实现的都是三栏布局,两边的盒子宽度固定,中间盒子自适应,也就是我们常说的固比固布局.它们实现的效果都是一样的,差别在于其实现的思想. 一.圣杯布局 html代码中,将重要的内容 ...

  4. Scrum立会报告+燃尽图(十一月十五日总第二十三次):代码规范与技术交流

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://git.coding.net/zhang ...

  5. 王者荣耀交流协会--第3次Scrum会议

    Scrum master:王玉玲 要求1:工作照片 要求2:时间跨度:2017年10月15号  6:00--6:24  共计24min要求3:地点:传媒西楼204,会议室要求4:立会内容:1.从昨日会 ...

  6. our team

    今天向大家介绍一下我们的团队,首先我们的团队叫“吉祥三宝”当然我们的三宝不是亲子关系,我们是兄弟关系,对,就是这样 下面来介绍一下我们的团队成团吧: 李奇原: 性格开朗.积极乐观.有责任心,擅长团队协 ...

  7. OpenNF tutorial复现

    这篇博客记录了自己实现OpenNF官网上tutorial的过程和遇见的问题,如果有不对的地方还请批评指正! tutorial链接 实验内容 这个实验展示了如何迅速且安全地把一个TCP流从一个NF实例迁 ...

  8. 软工 · BETA 版冲刺前准备(团队)

    软工 · BETA 版冲刺前准备(团队) 过去存在的问题 组员之间缺乏沟通,前后端缺乏沟通协作 组员积极性不高 基础知识不够扎实 手动整合代码效率过低 我们已经做了哪些调整/改进 通过会议加强组员之间 ...

  9. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  10. C语言调查问卷

    1.你对自己的未来有什么规划?做了哪些准备?毕业后应该不会从事编程类工作,目前有在学习感兴趣的东西.2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么?学习就是把不懂变成懂,可以充实自己.没 ...