10474:Where is the Marble?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/79948529

Where is the Marble?


排序加二分查找

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100000;
int n,q,mar[maxn];
int main(){
    int kase = 0;
    while(scanf("%d%d",&n,&q) == 2 && n){
        for(int i = 0;i < n;i++) scanf("%d",&mar[i]);
        sort(mar,mar + n);
        printf("CASE# %d:\n",++kase);
        int qnum,p;
        while(q--){
            scanf("%d",&qnum);
            p = lower_bound(mar,mar + n,qnum) - mar;
            if(mar[p] == qnum) printf("%d found at %d\n",qnum,p+1);//二分查找 不是 p == n
            else printf("%d not found\n",qnum);
        }
    }
    return 0;
}

阅读更多

Find the Marble

08-29

Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.nnUnfortunately, Alice's actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.nnInputnnThere are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.nnThe first line of each test case contains 4 integers n, m, k and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integers ai and bi (1 ≤ ai, bi ≤ n), telling the two pots Alice swaps in the i-th swapping.nnOutoutnnFor each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.nnSample Inputnn3n3 1 1 1n1 2n3 1 0 1n1 2n3 3 2 2n2 3n3 2n1 2nSample Outputnn2n1n3

Bridged Marble Rings

10-24

问题描述 :nn26 marbles―half yellow and half gray―are distributed between two circles of 13 marbles each. The marbles in each circle can be freely rotated clockwise or counterclockwise. The upper and lower circles are bridged by a smaller circle, which rotates―in the plane of the board―180 degrees, effectively exchanging the three bottommost marbles of the upper circle with the three uppermost marbles of the lower one. The goal is to get all gray marbles to the upper circle and all yellow marbles to the lower one while minimizing the number of times the bridging circle is rotated.nn输入:nnThe input is a series of lines, where each line describes an initial board configuration. Each line is a permutation of 13 y’s and 13 g’s. The first half of the line describes the clockwise configuration of the upper circle, and the rest of the line describes the clockwise configuration of the lower one. Of course, each y corresponds to a yellow marble, and each g corresponds to a gray one.nThe input file will include multiple test cases. Each test case consists of a single line containing some permutation of the string y13g13. All lines (including the last one) are terminated with a newline. The newline immediately follows the last letter on the line.nn输出:nnThe input is a series of lines, where each line describes an initial board configuration. Each line is a permutation of 13 y’s and 13 g’s. The first half of the line describes the clockwise configuration of the upper circle, and the rest of the line describes the clockwise configuration of the lower one. Of course, each y corresponds to a yellow marble, and each g corresponds to a gray one.nThe input file will include multiple test cases. Each test case consists of a single line containing some permutation of the string y13g13. All lines (including the last one) are terminated with a newline. The newline immediately follows the last letter on the line.nn样例输入:nngggggggggggggyyyyyyyyyyyyynyyyyyggggggggyyyygggggyyyyngyyygyggyyygyyggyyggggyyggnygygygygygygygygygygygygygn样例输出:nn0n2n5n6

没有更多推荐了,返回首页