程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces Round #287 (Div. 2)A,B,C,D,E

Codeforces Round #287 (Div. 2)A,B,C,D,E

編輯:關於C++

A簽到題排序之後貪心一下就可以了。

const int maxn = 10010;

using namespace std;

struct node
{
    int pos;
    int num;
}f[maxn];

bool cmp(node a, node b)
{
    return a.num < b.num;
}
int p[maxn];
int main()
{
    int n, k;
    while(cin >>n>>k)
    {
        for(int i = 0; i < n; i++)
        {
            cin >>f[i].num;
            f[i].pos = i+1;
        }
        sort(f, f+n, cmp);
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            if(k < f[i].num) break;
            p[ans++] = f[i].pos;
            k -= f[i].num;
        }
        cout<

B主要是策略每次沿著兩個圓心的連線轉就可以了,所以次數就是距離dis/2*r,注意處理精度,最後一組的好多人掛在精度上了。

const int maxn = 10010;

using namespace std;

int main()
{
    double r, x1, y1, x2, y2;
    while(cin >>r>>x1>>y1>>x2>>y2)
    {
        double dis = sqrt((x1-x2)*(x1-x2)*1.0 + 1.0*(y1-y2)*(y1-y2));
        LL xp = dis/(2.0*r);
        ///cout<<"st == "<

C樣例的圖解釋的很清楚了,按層枚舉之後你會發現規律,先判斷葉子的位置是在這一層的根節點的哪一邊,如果第k層的根節點x是奇數左子樹是的根節點是x+1,否則是x+2^(h-k+1),如果x是偶數那就反過來。

由於我的根節點是從一開始的,不要忘記了判斷最後一層就可以了啊。

C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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 test(s) input
    1 2
    
    output
    2
    input
    2 3
    
    output
    5
    input
    3 6
    
    output
    10
    input
    10 1024
    
    output
    2046
    Note

    A perfect binary tree of height h is a binary tree consisting of h?+?1 levels. Level 0 consists of a single node called root, level h consists of2h nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.

    Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.

    \

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define eps 1e-8
    #define M 1000100
    #define LL long long
    //#define LL long long
    #define INF 0x3f3f3f
    #define PI 3.1415926535898
    
    
    #define read() freopen("laser_maze.in", "r", stdin)
    #define write() freopen("laser_maze.out", "w", stdout);
    
    const int maxn = 10010;
    
    using namespace std;
    
    int main()
    {
        LL h, n;
        LL p;
        while(cin >>h>>n)
        {
            LL ans = h;
            p = 1;
            if(h == 1)
            {
                if(n == 1)
                {
                    cout<<1< xp)
                {
                    n -= xp;
                    if(p%2)
                    {
                        LL sp = (1LL<1 && p%2)p++;
            if(n == 1 && p%2 == 0) p++;
            cout<
    D,數位dp,當時讀題的時候讀錯了。題意是n位的數字,如果存在他的後綴%k=0,就算一種,求出總數來再mod m 就是結果。dp[i][j][k],代表第i位余數為j時他是否已經存在後綴串整除了,0代表不存在,1代表存在。

    D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    Amr doesn't like Maths as he finds it really boring, so he usually sleeps in Maths lectures. But one day the teacher suspected that Amr is sleeping and asked him a question to make sure he wasn't.

    First he gave Amr two positive integers n and k. Then he asked Amr, how many integer numbers x?>?0 exist such that:

    • Decimal representation of x (without leading zeroes) consists of exactly n digits;
    • There exists some integer y?>?0 such that:
      • \;
      • decimal representation of y is a suffix of decimal representation of x.

        As the answer to this question may be pretty huge the teacher asked Amr to output only its remainder modulo a number m.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CkNhbiB5b3UgaGVscCBBbXIgZXNjYXBlIHRoaXMgZW1iYXJyYXNzaW5nIHNpdHVhdGlvbj88L3A+CgoKCklucHV0CjxwPgpJbnB1dCBjb25zaXN0cyBvZiB0aHJlZSBpbnRlZ2VycyA8ZW0+bjwvZW0+LD88ZW0+azwvZW0+LD88ZW0+bTwvZW0+ICgxP6HcPzxlbT5uPC9lbT4/odw/MTAwMCwgMT+h3D88ZW0+azwvZW0+P6HcPzEwMCwgMT+h3D88ZW0+bTwvZW0+P6HcPzEwOSkuPC9wPgoKCgpPdXRwdXQKPHA+ClByaW50IHRoZSByZXF1aXJlZCBudW1iZXIgbW9kdWxvIDxlbT5tPC9lbT4uPC9wPgoKCgpTYW1wbGUgdGVzdChzKQoKCgppbnB1dAo8cHJlIGNsYXNzPQ=="brush:java;">1 2 1000

    output
    4
    input
    2 2 1000
    
    output
    45
    input
    5 3 1103
    
    output
    590
    Note

    A suffix of a string S is a non-empty string that can be obtained by removing some number (possibly, zero) of first characters from S.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define eps 1e-9
    ///#define M 1000100
    ///#define LL __int64
    #define LL long long
    ///#define INF 0x7ffffff
    #define INF 0x3f3f3f3f
    #define PI 3.1415926535898
    #define zero(x) ((fabs(x)>= 1;
        }
        return b;
    }
    int main()
    {
        LL n, k, m;
        while(cin >>n>>k>>m)
        {
            memset(dp, 0, sizeof(dp));
            dp[0][0][0] = 1;
            for(int i = 1; i <= n; i++)
            {
                int j = 0;
                if(i == n) j++;
                for(; j < 10; j++)
                {
                    for(int sk = 0; sk < k; sk++)
                    {
                        ///LL xp = (sk+j*10LL)%k;
                        LL xp = (pow_mod(10LL,i-1,k)*j%k+sk)%k;
                        if(!xp && j) dp[i][xp][1] += dp[i-1][sk][0];
                        else dp[i][xp][0] += dp[i-1][sk][0];
                        dp[i][xp][1] %= m;
                        dp[i][xp][0] %= m;
                        dp[i][xp][1] += dp[i-1][sk][1];
                        dp[i][xp][1] %= m;
                    }
                }
            }
            LL ans = 0;
            for(int i = 0; i < k; i++)
            {
                ans += dp[n][i][1];
                ans %= m;
            }
            cout<
    E這題感覺比D題簡單,就是有n個城市,m條邊。讓你求出來1到n的最短路,但是要求,保證最短路的時候,要求權值最大。

    E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

    Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.

    Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n cities with m bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads.

    The roads aren't all working. There are some roads which need some more work to be performed to be completely functioning.

    The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city n. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan.

    First of all the path which they are going to use on their way back from city 1 to their headquarters n must be as short as possible, since it is important to finish operation as fast as possible.

    Then, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional.

    If the chosen path has some roads that doesn't work they'll have to repair those roads before the operation.

    Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).

    Can you help Walter complete his task and gain the gang's trust?

    Input

    The first line of input contains two integers n,?m (2?≤?n?≤?105, \), the number of cities and number of roads respectively.

    In following m lines there are descriptions of roads. Each description consists of three integers x,?y,?z (1?≤?x,?y?≤?n, \) meaning that there is a road connecting cities number x and y. If z?=?1, this road is working, otherwise it is not.

    Output

    In the first line output one integer k, the minimum possible number of roads affected by gang.

    In the following k lines output three integers describing roads that should be affected. Each line should contain three integers x,?y,?z (1?≤?x,?y?≤?n, \), cities connected by a road and the new state of a road. z?=?1 indicates that the road between cities x and yshould be repaired and z?=?0 means that road should be blown up.

    You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it"s original state should be different from z.

    After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and n.

    If there are multiple optimal answers output any.

    Sample test(s) input
    2 1
    1 2 0
    
    output
    1
    1 2 1
    
    input
    4 4
    1 2 1
    1 3 0
    2 3 1
    3 4 1
    
    output
    3
    1 2 0
    1 3 1
    2 3 0
    
    input
    8 9
    1 2 0
    8 3 0
    2 3 1
    1 4 1
    8 7 0
    1 5 1
    4 6 1
    5 7 0
    6 8 0
    
    output
    3
    2 3 0
    1 5 0
    6 8 1
    
    Note

    In the first test the only path is 1?-?2

    In the second test the only shortest path is 1?-?3?-?4

    In the third test there are multiple shortest paths but the optimal is 1?-?4?-?6?-?8

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define eps 1e-9
    ///#define M 1000100
    ///#define LL __int64
    #define LL long long
    ///#define INF 0x7ffffff
    #define INF 0x3f3f3f3f
    #define PI 3.1415926535898
    #define zero(x) ((fabs(x)que;
        int n = y;
        for(int i = 1; i <= n; i++)
        {
            p[i].dis = INF;
            p[i].dw = INF;
        }
    
        p[x].dis = 0;
        p[x].dw = 0;
        flag[x] = true;
        que.push(x);
        while(!que.empty())
        {
            int sx = que.front();
            flag[sx] = false;
            que.pop();
            for(int i = head[sx]; i != -1; i = f[i].next)
            {
                int u = f[i].u;
                int w = f[i].w;
                if(p[u].dis > p[sx].dis+1)
                {
                    p[u].dis = p[sx].dis+1;
                    pre[u] = sx;
                    xpre[u] = i;
                    p[u].dw = p[sx].dw+w;
                    if(flag[u]) continue;
                    que.push(u);
                    flag[u] = true;
                }
                else if(p[u].dis == p[sx].dis+1 && p[u].dw > p[sx].dw+w)
                {
                    pre[u] = sx;
                    xpre[u] = i;
                    p[u].dw = p[sx].dw+w;
                    if(flag[u]) continue;
                    que.push(u);
                    flag[u] = true;
                }
            }
        }
    
        int xp = n;
        while(xpre[xp] != -1)
        {
            vis[xpre[xp]] = 1;
            xp = pre[xp];
        }
    
    }
    
    int main()
    {
        int n, m;
        while(cin >>n>>m)
        {
            init();
            int x, y, z;
            for(int i = 0; i < m; i++)
            {
                scanf("%d %d %d", &x, &y, &z);
                if(z == 0)
                {
                    add(x, y, z+2);
                    continue;
                }
                add(x, y, z);
            }
            bfs(1, n);
            int ans = 0;
            for(int i = 0; i < cnt; i += 2)
            {
                int x = f[i].u;
                int y = f[i+1].u;
                if(x > y) swap(x, y);
                int w = f[i].w;
                if(vis[i] || vis[i+1])
                {
                    if(w == 2)
                    {
                       tp[ans].x = x;
                       tp[ans].y = y;
                       tp[ans++].z = 1;
                    }
                }
                else
                {
                    if(w == 1)
                    {
                        tp[ans].x = x;
                       tp[ans].y = y;
                       tp[ans++].z = 0;
                    }
                }
            }
            cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved