Little Monkey Programming C++ | The light outside the school gate

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

There is a row of street lights on a road of length L outside the gate of a school   . The distance between every two adjacent lights is 1 meter. We can think of the road as a number axis. One end of the road is at the position of 0 on the number axis, and the other end is at the  position of L.  Each integer point on the number axis, that is, 0, 1, 2, ⋯, L  has a street light, which is numbered sequentially. is 0∼L .

Pulling the switch of each light can change in the order of off-red-yellow-green-blue-off. Initially, all street lights are off. Now there are  n  action sequences, each marking the meaning of the current action and the starting and ending positions. The specific meaning of each action is as follows:

  • 1 st: Turn   all the lights between  the s-  th light to the  t- th light (including the s  -th light and the  t-  th light) into the off state;
  • 2 st: Turn   all the lights on between the s-  th light and the  t -th light (including the  s-  th light and the  t  -th light) into red or green, and stop when the light turns red or green. Pull the switch;
  • 3 st: Turn   all the lights between the  s-  th light and the  t -th light (including the s-  th light and the  t  -th light) into blue or yellow, and stop pulling as soon as the light turns blue or yellow. switch;
  • 4 st: Pull   all the lights between the  s-  th light and the  t -th light (including the s-  th light and the  t-  th light), and pull the switch in the order of which light it is, and the position in this action  The light at s  is the first light, and   the light at position t is the last light.

Please calculate: the number of lights that are turned off when all sequences of actions are completed.

【enter】

The first line contains two integers  L , n .

Next  n  lines, each line contains three integers  op , s , t , where  op  represents the kind of action, and s and t  represent the start and end positions of the action respectively.

【Output】

A line containing an integer representing the answer.

【Input sample】

10 2
4 2 8
2 1 9

【Output sample】

5

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int l, n, op, s, t, ans=0;
int a[100005]={0};
int main()
{
    cin >> l >> n;
    for (int i=1; i<=n; i++) {
        cin >> op >> s >> t;
        if (op==1) {
            for (int i=s; i<=t; i++) {
                a[i] = 0;
            }
        } else if (op==2) {
            for (int i=s; i<=t; i++) {
                if (a[i]!=0) {
                    while (true) {
                        if (a[i]==1 || a[i]==3) break;
                        a[i] = (a[i]+1)%5;
                    }
                }
            }
        } else if (op==3) {
            for (int i=s; i<=t; i++) {
                while (true) {
                    if (a[i]==4 || a[i]==2) break;
                    a[i] = (a[i]+1)%5;
                }
            }
        } else if (op==4) {
            for (int i=s; i<=t; i++) {
                a[i] = (a[i]+ i-s+1)%5;
            }
        }
    }   
    for (int i=0; i<=l; i++) {
        if (a[i]==0) ans++;
    }
    cout << ans << endl;
    return 0;
}

【operation result】

10 2
4 2 8
2 1 9
5

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133930445