【Codeforces Global Round 1 E】Magic Stones

【链接】 我是链接,点我呀:)
【题意】

你可以把c[i]改成c[i+1]+c[i-1]-c[i]
(2<=i<=n-1)
问你能不能把每一个c[i]都换成对应的t[i];

【题解】

            d[i] = c[i+1]-c[i]; (1<=i<=n-1)

            change c[i]

            c[i]' = c[i+1]+c[i-1]-c[i];

            d[i-1] = c[i]'-c[i-1];
                   = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i];

            d[i] = c[i+1]-c[i]'
                 = c[i+1]-c[i+1]-c[i-1]+c[i]
                 = c[i] - c[i-1];
                 = d[i-1];
    也就是说对2..n-1进行操作的话
    就是把相邻的d的值交换一下
        显然这样的交换能让d变成任意顺序
    因此,只要c[1]==d[1]并且c[n]==d[n]
    然后排序后的c和排序后的d相同的话,就ok.
   这里必须要用Integer的equals方法比较才不会超时。。
   直接用int的a[i]!=b[i]会超时>_<

【代码】

import java.io.*;
import java.util.*;

//code start from here
/*
    d[i] = c[i+1]-c[i]; (1<=i<=n-1)

    change c[i]

    c[i]' = c[i+1]+c[i-1]-c[i];

    d[i-1] = c[i]'-c[i-1];
           = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i];

    d[i] = c[i+1]-c[i]'
         = c[i+1]-c[i+1]-c[i-1]+c[i]
         = c[i] - c[i-1];
         = d[i-1];
 */

public class Main {

    final static int N = (int)1e5;
    static InputReader in;
    static PrintWriter out;
    static int n;
    static Integer c[],t[],d1[],d2[];

    public static void main(String[] args) throws IOException{
        in = new InputReader();
        //out = new PrintWriter(System.out);
        c = new Integer[N+10];t = new Integer[N+10];
        d1 = new Integer[N+10]; d2 = new Integer[N+10];

        n = in.nextInt();
        for (int i = 1;i <= n;i++) c[i] = in.nextInt();
        for (int i = 1;i <= n;i++) t[i] = in.nextInt();
        if (!c[1].equals(t[1]) || !c[n].equals(t[n])){
            System.out.println("No");
            return;
        }
        for (int i = 1;i <= n-1;i++) d1[i] = c[i+1]-c[i];
        for (int i = 1;i <= n-1;i++) d2[i] = t[i+1]-t[i];
        Arrays.sort(d1, 1,n);
        Arrays.sort(d2, 1,n);
        for (int i = 1;i <= n-1;i++)
            if (!d1[i].equals(d2[i])) {
                System.out.println("No");
                return;
            }
        System.out.println("Yes");
    }

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in),32768);
            tokenizer = null;
        }

        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

原文地址:https://www.cnblogs.com/AWCXV/p/10357682.html

时间: 2024-08-01 01:35:57

【Codeforces Global Round 1 E】Magic Stones的相关文章

【 Codeforces Global Round 1 B】Tape

[链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即包含每一个点) 然后因为k<=n 所以可能胶带不够用. 那么就得一个胶带跨过两个点. 怎么选择最好呢? 可以把b[i]-b[i-1]-1处理出来排个序. (优先取较小的花费) 然后取前n-k个累加和sum. 因为每取一个就少用一段胶带. 然后sum+n就是答案了 [代码] import java.i

【Codeforces Global Round 1 C】Meaningless Operations

[链接] 我是链接,点我呀:) [题意] 给你一个a 让你从1..a-1的范围中选择一个b 使得gcd(a^b,a&b)的值最大 [题解] 显然如果a的二进制中有0的话. 那么我们就让选择的b的二进制中对应的位置为1 剩下全为0就好 这样a的二进制全都变成1之后就是答案了(gcd的右边是0). 但是如果a的二进制里面全是1的话. 就没办法这么构造了 这里有两种情况. ①.1的个数是偶数 那么就101010这样构造 另外一个数就是010101 答案就是010101转换成十进制 ②.1的个数是奇数

【Codeforces Global Round 1 A】Parity

[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] import java.io.*; import java.util.*; public class Main { static int N = (int)1e5; static InputReader in; static PrintWriter out; static int b,k; static

【手抖康复训练1 】Codeforces Global Round 6

[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思路手抖过不了样例,C题秒出思路手抖过不了样例*3 D题 手抖 过的了样例 ,调了1h,赛后发现变量名写错了,改一个字符就能AC... 题目等补完题一起放上来QAQ 原文地址:https://www.cnblogs.com/ttttttttrx/p/12110199.html

Codeforces Global Round 1 (A-E题解)

Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^(k-1)+a2*b^(k-2)+...+ak*b^0的奇偶性. 题解: 暴力求模2意义下的值就好了. 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5; int

Codeforces Global Round 1

A. Parity 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 int b, k, a[N]; 6 7 int main() 8 { 9 while (scanf("%d%d", &b, &k) != EOF) 10 { 11 int res = 0; 12 for (int i = 1; i <= k; ++i) scanf("%d&

【CF1110E】 Magic Stones - 差分

题面 Grigory has n n magic stones, conveniently numbered from \(1\) to \(n\). The charge of the \(i\)-th stone is equal to \(c_i\). Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index \(i\), where \(2 \le i \le n-1

Codeforces Global Round 7【ABCD】(题解)

目录 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Numbers B - Maximums C - Permutation Partitions D2 - Prefix-Suffix Palindrome (Hard version) 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Nu

【Henu ACM Round #13 C】 Ebony and Ivory

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先求出c-bx的所有可能 ->存在map里面 然后枚举y看看ay在不在map里面 在的话就有解. 这样复杂度是\(O(N*log_2N)\)的 比直接两层循环枚举的\(O(N^2)\)复杂度要来的好 这种方法也叫"中途相遇法" [代码] #include <bits/stdc++.h> #define ll long long using namespace std; ll a,b,c; map<