zoukankan      html  css  js  c++  java
  • Light bulbs------The Preliminary Contest for ICPC Asia Shanghai 2019

    There are NN light bulbs indexed from 00 to N-1N1. Initially, all of them are off.

    A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)FLIP(L,R) means to flip all bulbs xxsuch that L leq x leq RLxR. So for example, FLIP(3, 5)FLIP(3,5) means to flip bulbs 33 , 44 and 55, and FLIP(5, 5)FLIP(5,5) means to flip bulb 55.

    Given the value of NN and a sequence of MM flips, count the number of light bulbs that will be on at the end state.

    InputFile

    The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers NN and MM, the number of light bulbs and the number of operations, respectively. Then, there are MM more lines, the ii-th of which contains the two integers L_iLi and R_iRi, indicating that the ii-th operation would like to flip all the bulbs from L_iLi to R_iRi , inclusive.

    1 leq T leq 10001T1000

    1 leq N leq 10^61N106

    1 leq M leq 10001M1000

    0 leq L_i leq R_i leq N-10LiRiN1

    OutputFile

    For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the number of light bulbs that will be on at the end state, as described above.

    样例输入

    2
    10 2 2 6 4 8 6 3 1 1 2 3 3 4

    样例输出

    Case #1: 4

    Case #2: 3

    初始代码:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 
     6 struct node
     7 {
     8     int data;
     9     bool flag;
    10 }str[20006];
    11 
    12 bool cmp(struct node a, struct node b)
    13 {
    14     if(a.data!=b.data) return a.data <= b.data;
    15     else return a.flag==true;
    16 }
    17 
    18 int main()
    19 {
    20     int t, m, n, i, tot, top, x, pro, loc, re;
    21     scanf("%d", &t);
    22     tot = 0;
    23     while(t--)
    24     {
    25         scanf("%d %d", &n, &m);
    26         top = 0;
    27         re = 0;
    28         for(i=0;i<m;i++)
    29         {
    30             scanf("%d %d", &str[top].data, &str[top+1].data);
    31             str[top+1].data++;
    32             str[top].flag = true;
    33             str[top+1].flag = false;
    34             top+=2;
    35         }
    36 
    37         sort(str, str+top, cmp);
    38 
    39         x = 0;
    40         pro = str[0].data;
    41 
    42         loc = 0;
    43         while(loc<top)
    44         {
    45             if(str[loc].flag==true)
    46             {
    47                 x++;
    48                 if(x%2==1) re = re + str[loc].data - pro;
    49                 pro = str[loc].data;
    50                 loc++;
    51                 while(str[loc].data==pro)
    52                 {
    53                     if(str[loc].flag==true) x++;
    54                     else x--;
    55                     pro = str[loc].data;
    56                     loc++;
    57                 }
    58 
    59                 if(x%2==1) re = re + str[loc].data - pro;
    60                 pro = str[loc].data;
    61             }
    62 
    63             else
    64             {
    65                 x--;
    66                 if(x%2==1) re = re + str[loc].data - pro;
    67                 pro = str[loc].data;
    68                 loc++;
    69                 while(str[loc].data==pro)
    70                 {
    71                     if(str[loc].flag==true) x++;
    72                     else x--;
    73                     pro = str[loc].data;
    74                     loc++;
    75                 }
    76 
    77                 if(x%2==1) re = re + str[loc].data - pro;
    78                 pro = str[loc].data;
    79             }
    80 
    81         }
    82         tot++;
    83         printf("Case #%d: %d
    ", tot, re);
    84     }
    85     return 0;
    86 }

    简洁版代码:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int t, m, n, i, tot, top, x, pro, loc, re;
     8     int a[2006];
     9     scanf("%d", &t);
    10     tot = 0;
    11     while(t--)
    12     {
    13         scanf("%d %d", &n, &m);
    14         top = 0;
    15         re = 0;
    16         for(i=0;i<m;i++)
    17         {
    18             scanf("%d %d", &a[top], &a[top+1]);
    19             a[top+1]++;
    20             top+=2;
    21         }
    22 
    23         sort(a, a+top);
    24 
    25         x = 0;
    26         pro = a[0];
    27 
    28         loc = 0;
    29         while(loc<top)
    30         {
    31                 x++;
    32                 if(x%2==1) re = re + a[loc] - pro;
    33                 pro = a[loc];
    34                 loc++;
    35                 while(a[loc]==pro)
    36                 {
    37                     x++;
    38                     pro = a[loc];
    39                     loc++;
    40                 }
    41 
    42                 if(x%2==1) re = re + a[loc] - pro;
    43                 pro = a[loc];
    44         }
    45         tot++;
    46         printf("Case #%d: %d
    ", tot, re);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    解决 git 同步时 Everything up-to-date
    vs2019 git Authentication failed for xxx
    vs2015发布项目到虚拟主机组策略阻止csc.exe程序问题
    vs2017 使用 reportviewer
    var,dynamic的用法
    水晶报表报无法在资源中找到报表,请重新创建项目 错误
    css隐藏元素的方法
    css-浮动与清除浮动的原理详解(清除浮动的原理你知道吗)
    正则并不适合严格查找子串
    浏览器加载、渲染过程总结
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11528841.html
Copyright © 2011-2022 走看看