13. Shopping

桃扇骨 2022-04-10 04:22 301阅读 0赞

Description

Andy needs your help now! There are shops located on the Da’Jiao Road of Liang’Xiang. She knows that theth mall will be opening from moment time till moment

Imaging that she is the owner of the Da’Jiao Road, she wants to close one of the mall in order to save money. Lets call some malls naive if after close it the number of integer moments of time when at least one of malls is opening won’t decrease.

People in Liang’Xiang will be very upset if Andy has to close an excited mall. Help Andy by telling her the index of some naive mall. If there is no any, please print -1.

Input

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTIwNzE3NQ_size_16_color_FFFFFF_t_70

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. struct _shop
  4. {
  5. long open;
  6. long close;
  7. long num;
  8. }shop[200005];
  9. int comp(const void*p1, const void*p2)
  10. {
  11. struct _shop *c = (struct _shop*)p1;
  12. struct _shop *d = (struct _shop*)p2;
  13. if(c->open != d->open)
  14. return c->open - d->open;
  15. else
  16. return c->close - d->close;
  17. }
  18. int main(int argc, char const *argv[])
  19. {
  20. int flag = 0;
  21. long n;
  22. scanf("%ld", &n);
  23. for (long i = 0; i < n; i++)
  24. {
  25. scanf("%ld %ld", &shop[i].open, &shop[i].close);
  26. shop[i].num = i+1;
  27. }
  28. if(n == 1)
  29. {
  30. printf("-1\n");
  31. flag = 1;
  32. return 0;
  33. }
  34. qsort(shop, n, sizeof(struct _shop), comp);
  35. for (long i = 0; i < n; i++)
  36. {
  37. if(shop[i].open == shop[i+1].open)
  38. {
  39. printf("%ld\n", shop[i].num);
  40. flag = 1;
  41. break;
  42. }
  43. }
  44. if(flag == 0)
  45. {
  46. for (long i = 0; i < n - 2; i++)
  47. {
  48. if(shop[i+2].close > shop[i+1].close && shop[i].close+1 >= shop[i+2].open)
  49. {
  50. printf("%ld\n",shop[i+1].num);
  51. flag = 1;
  52. break;
  53. }
  54. if(shop[i].close >= shop[i+1].close)
  55. {
  56. printf("%ld\n",shop[i+1].num);
  57. flag = 1;
  58. break;
  59. }
  60. if(shop[i+1].close >= shop[i+2].close)
  61. {
  62. printf("%ld\n",shop[i+2].num);
  63. flag = 1;
  64. break;
  65. }
  66. if(shop[i].close >= shop[i+2].close)
  67. {
  68. printf("%ld\n",shop[i+2].num);
  69. flag = 1;
  70. break;
  71. }
  72. }
  73. }
  74. if(flag == 0)
  75. printf("-1\n");
  76. return 0;
  77. }

发表评论

表情:
评论列表 (有 0 条评论,301人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java实战——落饼Shop(WEB版)

    我终于想起还有CSDN这个东西,来丰富一波为数不多的公主号代码库~ 今天带来的是WEB版的小商城(PS:GZH里头有前端的完整代码提供,下面讲的东西包含后端(继续PS:清空购