zoukankan      html  css  js  c++  java
  • Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈

    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.

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

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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<queue>
     6 #include<map>
     7 #include<vector>
     8 #include<algorithm>
     9 using namespace std;
    10 #define ll __int64
    11 int a[5][5];
    12 int main()
    13 {
    14     for(int i=0;i<4;i++)
    15     {
    16         for(int j=0;j<4;j++)
    17             scanf("%d",&a[i][j]);
    18     }
    19     for(int i=0;i<4;i++)
    20     {
    21         if(a[i][3]==1)
    22         {
    23             for(int j=0;j<3;j++)
    24             {
    25                 if(a[i][j]==1)
    26                 {
    27                     printf("YES
    ");
    28                     return 0;
    29                 }
    30             }
    31                 if(a[(i+1)%4][0]==1||a[(i+2)%4][1]==1||a[(i+3)%4][2]==1)
    32                 {
    33                     printf("YES
    ");
    34                     return 0;
    35                 }
    36         }
    37     }
    38     printf("NO
    ");
    39     return 0;
    40 }
    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了 则不需要继续往上走 也不需要再回到楼梯所在的位置)

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<queue>
      6 #include<map>
      7 #include<vector>
      8 #include<algorithm>
      9 using namespace std;
     10 #define ll __int64
     11 int n,m;
     12 char a[20][105];
     13 int main()
     14 {
     15     scanf("%d %d",&n,&m);
     16     for(int i=0; i<n; i++)
     17         scanf("%s",a[i]);
     18     int exm=1<<n;
     19     int bit[20];
     20     int maxn=1000010000;
     21     int jishu=0;
     22     int flag;
     23     int zha=n-1;
     24     for(int i=0;i<n-1;i++)
     25     {
     26          flag=0;
     27         for(int k=0;k<m+2;k++)
     28         {
     29             if(a[i][k]=='1'&&flag==0)
     30             {
     31                 flag=1;
     32                 jishu=n-1-i;
     33                 zha=i;
     34             }
     35         }
     36         if(flag==1)
     37         break;
     38     }
     39     for(int i=0; i<exm; i++)
     40     {
     41         int len=0;
     42         int s=i;
     43         while(s>0)
     44         {
     45             bit[len++]=s%2;
     46             s/=2;
     47         }
     48         for(int j=len; j<n; j++)
     49             bit[j]=0;
     50         int now=0;
     51         int ans=jishu;
     52         int ji;
     53         for(int j=n-1; j>zha; j--)
     54         {
     55             if(now==0)
     56             {
     57                 ji=0;
     58                 if(bit[j]==0)
     59                 {
     60                     for(int k=1; k<m+1; k++)
     61                     {
     62                         if(a[j][k]=='1')
     63                             ji=k;
     64                     }
     65                     ans+=(ji*2);
     66                     now=bit[j];
     67                 }
     68                 else
     69                 {
     70                     ans+=(m+1);
     71                     now=bit[j];
     72                 }
     73             }
     74             else
     75             {
     76                 ji=m+1;
     77                 if(bit[j]==0)
     78                 {
     79                     ans+=(m+1);
     80                     now=bit[j];
     81                 }
     82                 else
     83                 {
     84                     for(int k=m; k>=1; k--)
     85                     {
     86                         if(a[j][k]=='1')
     87                             ji=k;
     88                     }
     89                     ans+=(m+1-ji)*2;
     90                     now=bit[j];
     91                 }
     92             }
     93         }
     94         if(now==0)
     95         {
     96             ji=0;
     97             for(int k=1; k<m+1; k++)
     98             {
     99                 if(a[zha][k]=='1')
    100                     ji=k;
    101             }
    102             ans+=ji;
    103         }
    104         else
    105         {
    106             ji=m+1;
    107             for(int k=m; k>=1; k--)
    108             {
    109                 if(a[zha][k]=='1')
    110                     ji=k;
    111             }
    112             ans+=(m+1-ji);
    113         }
    114         maxn=min(ans,maxn);
    115     }
    116     printf("%d
    ",maxn);
    117     return 0;
    118 }
    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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<queue>
     6 #include<map>
     7 #include<vector>
     8 #include<algorithm>
     9 #define ll __int64
    10 using namespace std;
    11 ll n,s;
    12 ll a[100005];
    13 ll b[100005];
    14 bool fun(ll k)
    15 {
    16     for(int i=1;i<=n;i++)
    17         b[i]=a[i]+i*k;
    18     sort(b+1,b+1+n);
    19     ll sum=0;
    20     for(int i=1;i<=k;i++)
    21         sum+=b[i];
    22     if(sum<=s)
    23         return true;
    24     else
    25         return false;
    26 
    27 }
    28 int main ()
    29 {
    30     scanf("%I64d %I64d",&n,&s);
    31     for(int i=1;i<=n;i++)
    32         scanf("%I64d",&a[i]);
    33     ll l=0,r=n,mid;
    34     ll ans;
    35     while(l<=r)
    36     {
    37         mid=(l+r)/2;
    38         if(fun(mid)){
    39          l=mid+1;
    40          ans=mid;
    41          }
    42          else
    43          r=mid-1;
    44     }
    45     cout<<ans<<" ";
    46     for(int i=1;i<=n;i++)
    47         b[i]=a[i]+i*ans;
    48     sort(b+1,b+1+n);
    49     ll sum=0;
    50     for(int i=1;i<=ans;i++)
    51         sum+=b[i];
    52     cout<<sum<<endl;
    53     return 0;
    54 }
    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博弈 然后考虑一下如何组合出答案。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<queue>
     6 #include<map>
     7 #include<vector>
     8 #include<algorithm>
     9 #define ll __int64
    10 using namespace std;
    11 ll  n;
    12 struct node
    13 {
    14     ll  pre;
    15     ll to;
    16 } N[400005];
    17 ll a[400005];
    18 ll nedge=0;
    19 ll pre[400005];
    20 ll used[400005];
    21 ll ji[400005],ji1;
    22 ll ou[400005],ou1;
    23 map<ll,ll>nm1;
    24 map<ll,ll>nm2;
    25 ll zong;
    26 void add(ll s,ll t)
    27 {
    28     nedge++;
    29     N[nedge].to=t;
    30     N[nedge].pre=pre[s];
    31     pre[s]=nedge;
    32 }
    33 void dfs(ll now,ll cen)
    34 {
    35     used[now]=1;
    36     if(cen%2==0)
    37     {
    38         ou[ou1++]=a[now];
    39         nm1[a[now]]++;
    40     }
    41     else
    42     {
    43         ji[ji1++]=a[now];
    44         nm2[a[now]]++;
    45     }
    46     for(int i=pre[now]; i; i=N[i].pre)
    47     {
    48         if(used[N[i].to]==0)
    49         {
    50             dfs(N[i].to,cen+1);
    51         }
    52     }
    53     zong=max(cen,zong);
    54 }
    55 int main()
    56 {
    57     scanf("%I64d",&n);
    58     ji1=0;
    59     ou1=0;
    60      zong=0;
    61     for(int i=1; i<=n; i++)
    62         scanf("%I64d",&a[i]);
    63     ll exm;
    64     for(int i=1; i<n; i++)
    65     {
    66         scanf("%I64d",&exm);
    67         add(i+1,exm);
    68         add(exm,i+1);
    69     }
    70     dfs(1,0);
    71     ll sum1=0,sum2=0;
    72     for(int i=0; i<ji1; i++)
    73         sum1^=ji[i];
    74     for(int i=0; i<ou1; i++)
    75         sum2^=ou[i];
    76     ll ans=0;
    77     if(zong%2==1)
    78     {
    79         if(sum1==0)
    80         ans=ans+(ll)ji1*(ji1-1)/2+(ll)ou1*(ou1-1)/2;
    81         for(int i=0; i<ji1; i++)
    82         {
    83             int exm=ji[i]^sum1;
    84             ans=ans+nm1[exm];
    85         }
    86     }
    87     else
    88     {
    89         if(sum2==0)
    90             ans=ans+ji1*(ji1-1)/2+ou1*(ou1-1)/2;
    91         for(int i=0; i<ou1; i++)
    92         {
    93             int exm=ou[i]^sum2;
    94             ans=ans+nm2[exm];
    95         }
    96     }
    97     printf("%I64d
    ",ans);
    98     return 0;
    99 }
  • 相关阅读:
    2006年百度之星程序设计大赛试题初赛题目题4剪刀石头布
    2006年百度之星程序设计大赛试题初赛题目题5座位调整
    Linux2.6用户空间堆栈区的分配与回收
    Linux2.6物理内存管理
    2006年百度之星程序设计大赛试题初赛题目题6百度语言翻译机
    带权的二分匹配
    二分图带权匹配KuhnMunkres算法(有修改)
    Linux2.6虚拟内存管理
    Linux2.6为数据结构分配内存slab
    2012225面试题目
  • 原文地址:https://www.cnblogs.com/hsd-/p/6986292.html
Copyright © 2011-2022 走看看