1486C2 - Guessing the Greatest (hard version)

该篇文章介绍了一种算法问题,即如何在给定的区间[L,R]内找到第二大的数的位置。通过询问区间并应用二分搜索,可以有效地缩小查找范围。首先确定最大值可能的边界,然后不断用二分查询更新左右边界,最终找到第二大的数的位置。
摘要由CSDN通过智能技术生成

题意就是我们询问区间 他会给第二大的数的位置。
琢磨下 可以想到 [L,x ,R] 。。
我们可以猜 最大值 在左边
我们再次进行询问 [L,x] 如果答案还是x 说明最大值肯定在左边 反之。。
但是这样还不够。还是会超出次数。
他这里进行了一种很巧妙的二分思想。。
我们一直保留x的位置。。作为边界。
我们对L->x这个区间反复进行二分询问 [mid,x];


#include <bits/stdc++.h>
using namespace std;
#define easy_code ios::sync_with_stdio(0)
#define endl '\n'
#define int long long
#define ar array<int, 2>
#define arr array<int, 3>
int T, n, m, k, inf = 1e18;
int mod = 998244353; // 1e9+7;
const int N = 201314;
bool query(int l, int r, int x)
{
    cout << "? " << l << " " << r << endl;
    int t;
    cin >> t;
    return t == x;
};
signed main()
{
    easy_code;
#ifdef DEBUG
    freopen("../1.in", "r", stdin);
#endif
    cin >> n;
    int l = 1, r = n;
    cout << "? " << l << " " << r << endl;
    int x;
    cin >> x;
    //下面两套二分框架是有区别的
    //核心区别在于 当合法的时候 我们是要把mid位置保留给L 还是R?
    if (x > 1 && query(1, x, x))
    {
        r = x - 1;
        while (l < r)//
        {
            int mid = (l + r + 1) >> 1;
            if (query(mid, x, x))
                l = mid;//因为这时候x在右边 所以当合法时候我们要保留mid 就把L边界移动到mid为位置。
            else
                r = mid - 1;
        }
    }
    else
    {
        l = x + 1;
        while (l < r)
        {
            int mid = (l + r) >> 1;
            if (query(x, mid, x))
                r = mid;//这变是x在左边界的位置。所以当合法的时候我们要把右边界移动到mid为止 不能-1 因为mid位置是有可能为最大值的。
            else
                l = mid + 1;
        }
    }
    cout << "! " << l;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Sure, I can help you with that. Here is the solution to your problem: GuessingGame.java ``` public class GuessingGame { private String secretWord; private StringBuilder currentGuess; private int remainingAttempts; public GuessingGame(String word) { secretWord = word; currentGuess = new StringBuilder("*".repeat(word.length())); remainingAttempts = 10; } public String getCurrentGuess() { return currentGuess.toString(); } public int getRemainingAttempts() { return remainingAttempts; } public boolean guessLetter(char letter) { boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == letter) { currentGuess.setCharAt(i, letter); correctGuess = true; } } if (!correctGuess) { remainingAttempts--; } return correctGuess; } public boolean isGameWon() { return currentGuess.indexOf("*") == -1; } public boolean isGameOver() { return remainingAttempts == 0; } } ``` Main.java ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a secret word: "); String secretWord = scanner.nextLine(); GuessingGame game = new GuessingGame(secretWord); while (!game.isGameOver()) { System.out.println("Current guess: " + game.getCurrentGuess()); System.out.println("Remaining attempts: " + game.getRemainingAttempts()); System.out.print("Enter a letter: "); char letter = scanner.nextLine().charAt(0); if (game.guessLetter(letter)) { System.out.println("Correct guess!"); if (game.isGameWon()) { System.out.println("You win!"); return; } } else { System.out.println("Incorrect guess!"); } } System.out.println("Game over! The secret word was: " + secretWord); } } ``` The `GuessingGame` class represents the game logic. It stores the secret word, the current guess, and the remaining attempts. It also provides methods to make a guess, check if the game is won or over, and retrieve the current guess and remaining attempts. The `Main` class is the entry point of the application. It prompts the user for a secret word, creates a `GuessingGame` instance, and starts a loop that allows the user to make guesses until the game is won or over. Each iteration of the loop displays the current guess, the remaining attempts, and prompts the user for a letter. It then calls the `guessLetter` method of the `GuessingGame` instance and displays a message depending on whether the guess was correct or not. If the game is won or over, it displays an appropriate message and exits the loop. Note that the `Main` class assumes that the user enters a valid input (i.e., a single letter) and doesn't handle any exceptions. You may want to add error handling or input validation code to make the application more robust.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值