Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈
1 second
256 megabytes
standard input
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.
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.
On a single line, print "YES" if an accident is possible, and "NO" otherwise.
1 0 0 1
0 1 0 0
0 0 1 0
0 0 0 1
YES
0 1 1 0
1 0 1 0
1 1 0 0
0 0 0 1
NO
1 0 0 0
0 0 0 1
0 0 0 0
1 0 1 0
NO
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 ;
}
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the minimum total time needed to turn off all the lights.
2 2
0010
0100
5
3 4
001000
000010
000010
12
4 3
01110
01110
01110
01110
18
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 ;
}
2 seconds
256 megabytes
standard input
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?
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.
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.
3 11
2 3 5
2 11
4 100
1 2 5 6
4 54
1 7
7
0 0
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 ;
}
2 seconds
256 megabytes
standard input
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:
- eat the apples, if the node is a leaf.
- 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.
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.
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.
3
2 2 3
1 1
1
3
1 2 3
1 1
0
8
7 2 2 5 4 3 1 1
1 1 1 4 4 5 6
4
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 模拟 枚举 二分 阶梯博弈的更多相关文章
- 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 ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)
http://codeforces.com/contest/812/problem/D 题意: 现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果 ...
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...
- 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 ...
- [Codeforces Round#417 Div.2]
来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交 D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...
- Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad
[题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...
- 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 ...
- 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
随机推荐
- Consul 架构(译)
Consul 架构 此篇文章主要对consul的相关内部技术细节进行简要概述. »术语 代理 - 代理是指consul集群中运行的consul实例,通过执行 consul agent 命令来启动. 代 ...
- 238. [LeetCode] Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- leetcode个人题解——#48 rotage image
思路:本题要求不能利用额外的二维数组实现旋转,所以重点在于弄清矩阵旋转的数学方法. 我的方法是,首先按照副对角线进行对称,然后按照水平中轴线进行对称即可. class Solution { publi ...
- nhibernate中执行SQL语句
在有些时候,可能需要直接执行SQL语句.存储过程等,但nhibernate并没有提供一种让我们执行SQL语句的方法,不过可以通过一些间接的方法来实现. 下面给出一个在nhibernate中执行SQL语 ...
- 5.airflow问题
1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...
- 一个网页从输入URL到页面加载完的过程
过程概述 1.浏览器查找域名对应的IP地址 2.浏览器根据IP地址与服务器建立socket连接 3.浏览器与服务器通信:浏览器请求,服务器处理请求和响应 4.浏览器与服务器断开连接 具体过程 1.搜索 ...
- 第18次Scrum会议(10/30)【欢迎来怼】
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/10/30 17:19~17:38,总计19min.地点:东北师 ...
- HDU 5183 Negative and Positive (NP) 前缀和+哈希
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5183 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- ACM解题之在线翻译 Give Me the Number
Give Me the Number Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- java多线程之CAS原理
前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.下 ...