zoukankan      html  css  js  c++  java
  • LUXURY15

    A - Guess Your Way Out!
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

    Let's index all the leaf nodes from the left to the right from 1 to 2h. The exit is located at some node n where 1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!

    Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:

    • Character 'L' means "go to the left child of the current node";
    • Character 'R' means "go to the right child of the current node";
    • If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
    • If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
    • If he reached a leaf node that is not the exit, he returns to the parent of the current node;
    • If he reaches an exit, the game is finished.

    Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?

    Input

    Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).

    Output

    Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.

    Sample Input

    Input
    1 2
    Output
    2
    Input
    2 3
    Output
    5
    Input
    3 6
    Output
    10
    Input
    10 1024
    Output
    2046
    这道题厉害(把今天做比赛的小牛全部吓跑了||-_-),实际上就是道数学题。
    首先把当前他给的出口转换为完全二叉树上的叶子节点标号exit,然后你就可以O(50)的复杂度内求出exit的所有祖先ans[] ;
    然后你可以设path[],表示当前这个点是他父亲的左儿子(0),还是右儿子(1)。
    通过观察你可以得到这个结论,如果path[i] != path[i-1] ,那么你就把i-1的另一个儿子产生的那颗子树上的节点全部加上,
    反之的话这次只要加以。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll ;
     4 ll ans[100] ;
     5 bool path[100] ;
     6 ll h , n ;
     7 ll tot ;
     8 
     9 void solve () {
    10         //cout << "n = " << n << endl ;
    11         //cout << "h = " << h << endl ;
    12         ans[h] = n ;
    13         for (int i = h - 1 ; i >= 1 ; i --) ans[i] = ans[i+1]/2 ;
    14         if (ans[2] == 2) path[2] = 1 , tot += 2;
    15         else path[2] = 0 , tot += (1ll<<(h-1)) + 1 ;
    16         //printf ("ans[2] = %I64d , path[2] = %d
    " , ans[2] , path[2]) ;
    17         //cout << tot << endl ;
    18         for (int i = 3 ; i <= h ; i ++) {
    19                 if (ans[i-1] * 2 + 1 == ans[i]) path[i] = 0 ;
    20                 else path[i] = 1 ;
    21                 tot ++ ;
    22                 if (path[i] == path[i-1]) tot += (1ll<<(h-i+1)) - 1 ; 
    23                 //printf ("ans = %I64d , path[%d] = %d
    " , ans[i] , i , path[i]) ;
    24                 //cout << tot << endl ;
    25         }
    26         cout << tot - 1 << endl ;
    27 }
    28 
    29 int main () {
    30         cin >> h >> n ;
    31         if (h == 1 && n == 1) {
    32                 cout << 1 << endl ;
    33                 return 0 ;
    34         }
    35         n += (1ll<<h) - 1 ;
    36         h ++ ;
    37         solve () ;
    38         return 0 ;
    39 }
    View Code
    B - Case of Matryoshkas
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

    The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.

    In one second, you can perform one of the two following operations:

    • Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
    • Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out ofb.

    According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

    Input

    The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.

    The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbersai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).

    It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

    Output

    In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

    Sample Input

    Input
    3 2
    2 1 2
    1 3
    Output
    1
    Input
    7 3
    3 1 3 7
    2 2 5
    2 4 6
    Output
    10
    这道题乱入进来的,就是个模拟。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll ;
     4 const int M = 1e5 + 10 ;
     5 int n , k ;
     6 ll tot ;
     7 ll len ;
     8 int path[M] ;
     9 ll cnt ;
    10 
    11 int main () {
    12         ios::sync_with_stdio (false) ;
    13         cin >> n >> k ;
    14         tot = n-1 - (k-1) ;
    15         for (int i = 0 ; i < k ; i ++) {
    16                 int num ;
    17                 cin >> num ;
    18                 for (int i = 0 ; i < num ; i ++) cin >> path[i] ;
    19                 if (path[0] = 1) {
    20                        for (int i = 1 ; i < num ; i ++) {
    21                               if (path[i] == path[i-1] + 1) cnt ++ ;
    22                               else break ;
    23                        }
    24                 }
    25         }
    26         tot -= cnt ;
    27         tot += n - 1 - cnt ;
    28         cout << tot << endl ;
    29         return 0 ;
    30 }
    View Code
    C - Arthur and Table
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

    In total the table Arthur bought has n legs, the length of the i-th leg is li.

    Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

    A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

    Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

    Input

    The first line of the input contains integer n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.

    The second line of the input contains a sequence of n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.

    The third line of the input contains a sequence of n integers di (1 ≤ di ≤ 200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

    Output

    Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

    Sample Input

    Input
    2
    1 5
    3 2
    Output
    2
    Input
    3
    2 4 4
    1 1 1
    Output
    0
    Input
    6
    2 2 1 1 3 3
    4 3 5 5 2 1
    Output
    8

     暴力枚举1~1e5 ,使他们分别为最大桌腿时的需要消耗的能量,不断取最小。

    一大堆前缀和,后缀和混在一起。。。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int M = 1e5 + 10 , inf = 0x3f3f3f3f ;
     4 int vis[M] ;
     5 int engry[M] ;
     6 struct node {
     7         int all ;
     8         int num[M] ;
     9 } a[210] ;
    10 int n ;
    11 int len[M] ;
    12 pair<int,int> ure[M] ;
    13 
    14 void solve () {
    15         int minn = inf ;
    16         for (int i = 1 ; i <= M - 10 ; i ++) {
    17                 if (vis[i] == 0) continue ;
    18                 int tmp = ure[i+1].second ;
    19                 int cut = n - ure[i+1].first - (2*vis[i]-1) ;
    20                 if (cut <= 0) {
    21                         minn = min (minn , tmp) ;
    22                         continue ;
    23                 }
    24                 int cnt = 0 ;
    25                 for (int j = 1 ; j <= 200 ; j ++) {
    26                         int dif = cut - cnt ;
    27                         if (dif <= a[j].num[i-1]) {
    28                                 tmp += j * dif ;
    29                                 break ;
    30                         }
    31                         tmp += j * a[j].num[i-1];
    32                         cnt += a[j].num[i-1] ;
    33                 }
    34                 minn = min (minn , tmp) ;
    35         }
    36         cout << minn << endl ;
    37 }
    38 
    39 int main () {
    40         ios::sync_with_stdio (false) ;
    41         cin >> n ;
    42         for (int i = 0 ; i < n ; i ++) {
    43                 cin >> len[i] ;
    44                 vis[len[i]] ++ ;
    45         }
    46         for (int i = 0 ; i < n ; i ++) {
    47                 int x ;
    48                 cin >> x ;
    49                 a[x].all ++ ;
    50                 a[x].num[len[i]] ++ ;
    51                 engry[len[i]] += x ;
    52         }
    53         for (int i = M - 10 ; i >= 1 ; i --) ure[i].first += vis[i] + ure[i+1].first , ure[i].second += engry[i] + ure[i+1].second ;
    54         for (int i = 1 ; i <= 200 ; i ++) {
    55                 for (int j = 1 ; j <= M - 10 ; j ++) a[i].num[j] += a[i].num[j-1] ;
    56         }
    57         solve () ;
    58         return 0 ;
    59 }
    View Code
    D - Haar Features
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.

    Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.

    feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.

    To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The valueof a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.

    Some examples of the most popular Haar features are given below.

    Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.

    prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.

    You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable value.

    You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.

    Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.

    Output

    Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.

    Sample Input

    Input
    6 8
    BBBBBBBB
    BBBBBBBB
    BBBBBBBB
    WWWWWWWW
    WWWWWWWW
    WWWWWWWW
    Output
    2
    Input
    3 3
    WBW
    BWW
    WWW
    Output
    4
    Input
    3 6
    WWBBWW
    WWBBWW
    WWBBWW
    Output
    3
    Input
    4 4
    BBBB
    BBBB
    BBBB
    BBBW
    Output
    4

    Hint

    The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following twooperations:

    1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); 
    2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2and variable value

    Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 =  - 1, as required.

    这道题很神,题目读起来特费劲。简单来说是这样,一个图里有W,B这两种颜色,你可以每次进行一种操作:从左上角拉出一个矩阵来(你可以想象鼠标在那里右击一下然后拉出了一个矩阵),在那个矩阵里

    所有数字都加上一个任意大小的整数。进过这样若干次操作后,最终是w变成1,b变成-1.

    问你最少的操作次数。

    题解的思路真心好。首先你肯定会得出这么一个结论,一定要从矩阵最外围那里进行修改。

    然后做法是这样,首先把这幅图上的W变成1 , B变成-1,那么我们现在要做的就是怎么用最少的操作次数把现在的这张图变成一开始都是0的图了。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int mp[101][101] ;
     4 int n , m ;
     5 
     6 int main () {
     7         ios::sync_with_stdio (false) ;
     8         cin >> n >> m ;
     9         for (int i = 1 ; i <= n ; i ++) {
    10                 for (int j = 1 ; j <= m ; j ++) {
    11                         char x ;
    12                         cin >> x ;
    13                         mp[i][j] = x == 'B' ? 1 : -1 ;
    14                 }
    15         }
    16 
    17         int cnt = 0 ;
    18         for (int i = n ; i >= 1 ; i --) {
    19                 for (int j = m ; j >= 1 ; j --) {
    20                         if (mp[i][j] == 0) continue ;
    21                         cnt ++ ;
    22                         int tmp = mp[i][j] ;
    23                         for (int k = i ; k >= 1 ; k --) {
    24                                 for (int t = j ; t >= 1 ; t --) {
    25                                         mp[k][t] -= tmp ;
    26                                 }
    27                         }
    28                 }
    29         }
    30         cout << cnt << endl ;
    31         return 0 ;
    32 }
    View Code
    E - Kyoya and Colored Balls
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen.

    Input

    The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors.

    Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).

    The total number of balls doesn't exceed 1000.

    Output

    A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007.

    Sample Input

    Input
    3
    2
    2
    1
    Output
    3
    Input
    4
    1
    2
    3
    4
    Output
    1680

    Hint

    In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are:


    1 2 1 2 3
    1 1 2 2 3
    2 1 1 2 3

    虽然一开始就知道是dp,但我没有正确的确定当前的要转移的状态是什么。
    题解里说dp[i],表示只用前i种颜色的所有组合情况,那就很明显了,dp[i] = dp[i-1]*C(c[i]-1,pre[i]-1) ;
    用了组合数学的姿势,第一次写这种玩意,借鉴了一下大牛的打表姿势。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll ;
     4 const ll mod = 1e9 + 7 ;
     5 int k ;
     6 int a[2000] ;
     7 ll f[2000] ;
     8 ll pre[2000] ;
     9 ll c[1010][1010] ;
    10 
    11 int main () {
    12         ios::sync_with_stdio (false) ;
    13         cin >> k ;
    14         for (int i = 1 ; i <= k ; i ++) {
    15                 cin >> a[i] ;
    16                 pre[i] += pre[i-1] + a[i] ;
    17         }
    18         for (int i = 0 ; i <= 1000 ; i ++) {
    19                 c[i][0] = 1 ;
    20                 for (int j = 1 ; j <= i ; j ++) c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod ;
    21         }
    22         f[1] = 1 ;
    23         for (int i = 2 ; i <= k ; i ++) {
    24                 f[i] = f[i-1]*c[pre[i]-1][a[i]-1] % mod ;
    25                 //printf ("(%I64d , %d)
    " , pre[i]-1 , a[i]-1) ;
    26                 //cout << "f[" << i << "] = " << f[i] << endl ;
    27         }
    28         cout << f[k] << endl ;
    29         return 0 ;
    30 }
    View Code
     
     
  • 相关阅读:
    MVC @Html.TextBox 添加属性和样式
    当项目中出现“未能找到与此解决方案关联的源代码管理提供程序。项目将视为不受源代码管理”
    MVC视图中读取ViewBag传递过来的HashTable表数据
    使用NPOI导入导出标准的Excel
    网站文件下载代码
    C# 刷新页面浏览次数(点击量)+1
    sed
    Shell变量的取用、删除、取代与替换
    non-member function cannot have cv-qualifier
    awk学习
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4716382.html
Copyright © 2011-2022 走看看