CF 1114 E. Arithmetic Progression

E. Arithmetic Progression

链接

题意:

  交互题。

  有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差。每次可以询问是否有严格大于x的数,和查看一个位置的数。

分析:

  首先可以二分找到序列的最大值,然后考虑如何求公差。

  随机选30个数,然后对任意两个求一遍gcd即可。

  正确性证明

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int query(int ty,LL x) {
    int y;
    if (ty == 1) printf("> %I64d\n", x);
    else printf("? %I64d\n", x);
    fflush(stdout);
    cin >> y;
    if (y == -1) exit(0);
    return y;
}

vector<LL> vec;
int Rand() { return (rand() << 15) | rand(); }
int id[1000005];

int main() {
    srand(1114);
    int n; cin >> n;
    LL l = -1e9, r = 1e9, ans = 0, cnt = 60, tot = n;
    while (l <= r) {
        LL mid = (l + r) >> 1;
        cnt --;
        if (query(1, mid) == 0) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    for (int i = 1; i <= tot; ++i) id[i] = i;
    while (cnt > 0 && tot > 0) {
        int x = Rand() % tot + 1;
        vec.push_back(query(0, id[x]));
        swap(id[x], id[tot]);
        tot --;    cnt --;
    }
    sort(vec.begin(), vec.end());
    if (vec.back() != ans) vec.push_back(ans);
    LL d = vec[1] - vec[0];
    for (int i = 0; i < (int)vec.size() - 1; ++i) d = __gcd(d, vec[i + 1] - vec[i]);
    printf("! %I64d %I64d\n", ans - (n - 1) * d, d);
    return 0;
}

原文地址:https://www.cnblogs.com/mjtcn/p/10361325.html

时间: 2024-08-29 02:16:04

CF 1114 E. Arithmetic Progression的相关文章

HDOJ 5143 NPY and arithmetic progression DFS

DFS..... 多余3个的数可以自己成等比数列 和其他数组合成等比数列的有1,2,3,  2,3,4  1,2,3,4 三种情况 NPY and arithmetic progression Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 189    Accepted Submission(s): 61 Problem Descri

BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数只使用一次的前提下,分成若干部分,每部分数的长度 >= 3且满足是等差数列.可以的话输出 Yes ,否则 No . 比赛的时候过了pretest,那个开心啊--然后跟 XX 兽讨论了之后,才发现自己理解错了题目= = 要用到深搜,问组合嘛--组合就是有可能是(1, 2, 3, 4).(1, 2, 3

Find Missing Term in Arithmetic Progression 等差数列缺失项

查找等差数列中的缺失项. e.g.Input: arr[] = {2, 4, 8, 10, 12, 14} Output: 6 Input: arr[] = {1, 6, 11, 16, 21, 31}; Output: 26. 采用binary search. 若是arr[mid] - arr[left] == (mid - left) * diff, 说明missing 部分在mid右侧. 否则missing部分在包括mid的左侧. Time complexity: O(logn). Spa

CF 1114 D. Flood Fill

D. Flood Fill 链接 题意: 一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次.n<=5000 分析: 区间dp. dp[i][j][0/1]表示当前的区间是l~r,把这一段变成一个颜色的最少次数,最后一个改变的颜色是最左边/最右边的一段. 代码: #include<cstdio> #include<algorithm> #include<cstring> #inc

【leetcode】1228.Missing Number In Arithmetic Progression

题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(object): def missingNumber(self, arr): """ :type arr: List[int] :rtype: int """ arr.sort() diff = (arr[-1] - arr[0])/(len(arr)) fo

CodeForces Contest #1114: Round #538 (Div. 2)

比赛传送门:CF #1114. 比赛记录:点我. 又 FST 了. [A]Got Any Grapes? 题意简述: 有三个人,第一个人需要吃绿色葡萄至少 \(a\) 个,第二个人需要吃绿色和紫色葡萄至少 \(b\) 个,第三个人需要吃绿色.紫色和黑色葡萄至少 \(c\) 个. 有 \(x\) 个绿色葡萄,\(y\) 个紫色葡萄,\(z\) 个黑色葡萄,问是否能够满足三个人的要求. 题解: #include <cstdio> int main() { int x, y, z, a, b, c;

USACO 1.4 Arithmetic Progressions

Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer. Write a program that finds all arithmetic progression

URAL 1828. Approximation by a Progression 数学

1828. Approximation by a Progression Time limit: 0.5 second Memory limit: 64 MB Your are given a sequence of integers a1, -, an. Find an arithmetic progression b1, -, bn for which the value ∑(ai ? bi)2 is minimal. The elements of the progression can

CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)

题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants to know how many ways he can choose three numbers such that they are three consecutive terms of an arithmetic progression. Meaning that, how many trip