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. Angular7运行机制--根据腾讯课堂米斯特吴 《Angular4从入门到实战》学习笔记分析完成

  2. Delphi 中的 RectTracker - 原创

    本文算是副产品,正品是利用 FFmpeg 从任意视频中生成GIF片段的小程序,写完了就发. V2G 正品已出炉,虽然不大像样,但好歹是能用,请见:用 Delphi 7 实现基于 FFMS2 的视频转 ...

  3. java使用jacob将office文档转换为PDF格式

    jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...

  4. NO.2:自学python之路------变量类型、列表、字典

    引言 本周初步认识了库,并学习了Python中各种类型的变量和常用操作.并完成了较为完善的用户与商家购物界面设计. 正文 模块: Python有标准库和第三方库.第三方库需要安装才能使用.大量的库可以 ...

  5. UUID.randomUUID()简单介绍

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  6. Spark入门(Python)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  7. sshpass 指定密码远程 ssh 到服务器 或者 scp 发送文件到服务器

    在操作linux时,虽然可以对linux配置免秘钥登录,但是在配置免密码登录之前,是需要登录到其他节点主机的,这里提供一种类似ssh的方式,可以在命令后面加上相应的参数来设置你将要登录的远程主机的密码 ...

  8. react native组件的创建

    react native组件的创建 react文件加载顺序: react项目启动后,先加载index.js.在index.js中可以指向首页. import { AppRegistry } from ...

  9. 一个网页从输入URL到页面加载完的过程

    过程概述 1.浏览器查找域名对应的IP地址 2.浏览器根据IP地址与服务器建立socket连接 3.浏览器与服务器通信:浏览器请求,服务器处理请求和响应 4.浏览器与服务器断开连接 具体过程 1.搜索 ...

  10. Live Archive 训练题 2019/3/9

    7454 Parentheses A bracket is a punctuation mark, which is used in matched pairs, usually used withi ...