2016 7th Blue Bridge Cup Java Programming Undergraduate Group B Final Round Dance (Programming Topics)

2016 7th Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90169154

 

Question 6

Title: Circle Dance

Test address: https://www.docpp.com/oj/problem1836.html

The warm sunshine in spring shines on the earth, which is the happiest time for the little animals on the grassland. The little animals held a dance party on the grassland to celebrate this wonderful time.

The most important part of the dance is the waltz dance, where n small animals form a large circle hand in hand and dance to the music. During the jump, the animals may change formations. The way they change is that animal A releases its right hand, animal B releases its left hand, animals A and B pull their hands together, and their corresponding released hands (if any) also pull together.

For example, suppose there are 10 small animals and they form a circle in order. The right hand of animal 1 pulls the left hand of animal 2, the right hand of animal 2 pulls the left hand of animal 3, and so on. Finally, the right hand of animal 10 pulls animal 1. 's left hand. If animals 2 and 8 change the formation, the right hand of animal 2 pulls the left hand of animal 8, and the corresponding left hand of animal 3 pulls the right hand of animal 7, thus forming 1-2-8-9-10 and 3 -4-5-6-7 two laps. If the formation is changed by animals 2 and 6 at this time, a large circle of 1-2-6-7-3-4-5-8-9-10 will be formed. Note that if animal 1 and 2 change formation at this time, the formation will not change, because animal 1's right hand and animal 2's left hand are released and pulled together again.

In the process of dancing, each animal i has a joy value Hi and a moving value Fi.
If two animals are in a circle, the joy value will affect each other, producing joy energy. If two animals i, j (i≠j) are in the same circle of size t, and animal i is in the p-th position of animal j’s right hand (the first position of animal j’s right hand is the one held by animal j’s right hand) , and the second position is the animal in the first position of the right hand pulling the animal with the right hand, and so on), the resulting joy energy is (tp)*Hj*Fi. In the process of dancing, the joy and emotion of animals may change.

At the beginning of the waltz, all animals form a circle in numbered order, and the i-th position of animal n's right hand happens to be animal i. Now we know the process of changing the formation of small animals and the process of changing the joy value and emotion value. Find the sum of the welcome energy generated by all animals after each change.

【Input format】
The first line of input contains an integer n, which represents the number of animals.
The next n lines, each with two space-separated integers Hi, Fi, give each animal's joy value and moving value in numerical order.
The next line contains an integer m, which represents the number of changes in formation, joy value, and emotional value.
The next m lines, each line contains three integers k, p, q separated by spaces. When k=1, it means that the animals have changed their formation through animal p and animal q. When k=2, it means animal p The joy value of q becomes q, and when k=3, it means that the moving value of animal p becomes q.

[Output format]
Output m lines, each line is an integer, which represents the sum of the energy generated by all animals after each change.
The answer can be large, you need to calculate the remainder of dividing the answer by 1000000007.

【Sample input】
10
1 1
1 1
1 1 1
1 1 1
1 1
1 1
1
1
1 1
1 1
9
1 2 8
1 2 6
2 8 10
3 5 10
1 1 2
1 2 1
2 5 5
1 4 8
1 4 5

【Example output】
100
450
855
1341
1341
811
923
338
923

[Data scale and convention]
For 20% of the data, 2<=n, m<=100.
For 30% of the data, 2<=n,m<=1000.
Another 20% of the data has only k=1 operations and Hi and Fi are both 1.
Another 20% of the data has only k=1 or 2 operations and Fi is 1.
For 100% data, 2<=n, m<=100000, 0<=Hi, Fi<=10^9, 1<=k<=3, when k=1, 1<=p, q<=n and p≠q, 1<=p<=n and 0<=q<=10^9 when k=2 or 3.

Resource convention:
peak memory consumption < 256M
CPU consumption < 5000ms

Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

All code is placed in the same source file, after debugging, copy and submit the source code.
Note: Do not use the package statement. Do not use features of jdk1.7 and above.
Note: The name of the main class must be: Main, otherwise it will be processed as invalid code.


Solution: Just use the structure to simulate and then calculate violently. Pay attention to the formula for calculating happy energy. Don't make a mistake. The formula given in the title is the formula corresponding to the p-th position of animal i in the right hand of animal j . If it is calculated by traversal, it is the p-th position of animal j in the right hand of animal i. , then the i and j corresponding to the formula should also be exchanged.

Code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n, t, k, p, q;
    public static long mod = 1000000007;
    public static Person[] per = new Person[100010];
    public static HashSet<Integer> hs;

    public static void main(String[] args) {
        n = in.nextInt();
        for (int i = 1; i <= n; i++) {
            per[i] = new Person();
            per[i].h = in.nextLong();
            per[i].f = in.nextLong();
            per[i].left = (i-1==0) ? n : i-1;
            per[i].right = (i+1>n) ? 1 : i+1;
            per[i].num = n;
        }
        t = in.nextInt();
        while (t-- > 0) {
            k = in.nextInt();
            p = in.nextInt();
            q = in.nextInt();
            if (k == 1) {
                int oldleft = per[q].left, oldright = per[p].right;
                per[p].right = q;
                per[q].left = p;
                per[oldleft].right = oldright;
                per[oldright].left = oldleft;
                hs = new HashSet<>();
                for (int i = 1; i <= n; i++) {
                    if (hs.contains(i) == false) {
                        int sum = 1, tmp = i;
                        hs.add(i);
                        while (hs.contains(per[tmp].right) == false) {//遍历同一个圈子里的
                            hs.add(per[tmp].right);
                            sum++;
                            per[tmp].num = -1;
                            tmp = per[tmp].right;
                        }
                        per[tmp].num = -1;
                        per[i].num = sum;
                        tmp = per[i].right;
                        while (per[tmp].num == -1) {//更新每个圈子的人数
                            per[tmp].num = sum;
                            tmp = per[tmp].right;
                        }
                    }
                }
            } else if (k == 2) {
                per[p].h = Long.valueOf(q);
            } else {
                per[p].f = Long.valueOf(q);
            }
            out.println(getAns());
            out.flush();
        }
        out.close();
    }

    static class Person {
        long h, f;
        int left, right, num;
    }

    static long getAns() {
        long ans = 0;
        for (int i = 1; i <= n; i++) {
            int cnt = per[i].num - 1, now = 1, nowper = per[i].right;
            while (now <= cnt) {
                ans = (ans + (Long.valueOf((per[nowper].num - now))*per[nowper].f*per[i].h)%mod) % mod;
                nowper = per[nowper].right;
                now++;
            }
        }
        return ans;
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

        public BigDecimal nextBigDecimal() {
            return new BigDecimal(next());
        }

    }
}

Evaluation results:


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568052&siteId=291194637