fork download
  1. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
  2. //| AUTHOR - AnmolTomer |
  3. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
  4. //Compile: g++ -std=c++14 -O2 -Wall test.cpp -o test.out
  5. //Debug: g++ -g -std=c++14 -O2 -Wall test.cpp -o test.out
  6. // Problem Link: https://w...content-available-to-author-only...h.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/seven-segment-display-nov-easy-e7f87ce0/
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. typedef vector<int> vi;
  11. typedef pair<int, int> pii;
  12. #define endl "\n"
  13. #define sd(val) scanf("%d", &val)
  14. #define pd(val) printf("%d\n", val)
  15. #define REP(a, b) for (int i = a; i < b; i++)
  16. #define REP1(a, b) for (int i = a; i <= b; i++)
  17. #define frr(i, n) for (int i = 0; i < (n); i++)
  18. #define ss(val) scanf("%s", &val)
  19. #define sull(val) scanf("%llu", &val)
  20. #define sl(val) scanf("%lld", &val)
  21. #define debug(val) printf("check%d\n", val)
  22. #define all(v) v.begin(), v.end()
  23. #define pb push_back
  24. #define mp make_pair
  25. #define ff first
  26. #define sc second
  27. #define ll long long
  28. #define ull unsigned long long
  29. #define MOD 1000000007
  30. #define w(t) \
  31.   int t; \
  32.   cin >> t; \
  33.   while (t--)
  34. #define clr(val) memset(val, 0, sizeof(val))
  35. #define what_is(x) cerr << #x << " is " << x << endl;
  36. #define OJ \
  37.   freopen("input.txt", "r", stdin); \
  38.   freopen("output.txt", "w", stdout);
  39. #define FIO \
  40.   ios_base::sync_with_stdio(false); \
  41.   cin.tie(NULL); \
  42.   cout.tie(NULL);
  43.  
  44. // <=======END OF TEMPLATE=============>
  45. void solve();
  46. int main()
  47. {
  48. // OJ;
  49. // FIO;
  50. ios::sync_with_stdio(0);
  51. cin.tie(0);
  52. map<int, int> m;
  53. // 15673 will use 2+5+6+3+5 = 21 sticks
  54. // If total number of sticks == even then make a number using as many sticks of 1 as it is the smallest number e.g. instead of making nine with 6 sticks, you could make 111 with 6 sticks, as number one takes 2 sticks.
  55. // If numberofSticks == odd, then largest number with as few sticks that we can make is 7 which uses only 3 sticks, so subtract 3 from total sticks, that would make your totalSticks as even and then again use all the even number of sticks for making number with 1, something like this, say you have 11 sticks then you would make a 7 with 3 sticks leaving you with 8 sticks and with 8 sticks you can make 1111 and placing larges number 7 in front gives you largest number as 71111 using 11 sticks.
  56. m.insert(pii(0, 6));
  57. m.insert(pii(1, 2));
  58. m.insert(pii(2, 5));
  59. m.insert(pii(3, 5));
  60. m.insert(pii(4, 4));
  61. m.insert(pii(5, 5));
  62. m.insert(pii(6, 6));
  63. m.insert(pii(7, 3));
  64. m.insert(pii(8, 7));
  65. m.insert(pii(9, 6));
  66. w(t)
  67. {
  68.  
  69. int n;
  70. cin >> n;
  71. int totalSticks = 0;
  72.  
  73. while (n > 10)
  74. {
  75. for (auto e : m)
  76. {
  77. // cout << "N before n%10 = " << n % 10 << endl;
  78. if (e.first == (n % 10))
  79. totalSticks += e.second;
  80. }
  81. n /= 10;
  82. // cout << "N after n/10 = " << n << endl;
  83. }
  84. // cout << "N outside the loop: " << n << endl;
  85. for (auto e : m)
  86. {
  87. if (e.first == (n % 10))
  88. totalSticks += e.second;
  89. }
  90. // cout << "Final totalsticks = " << totalSticks << endl;
  91. ull int maxNumber = 0;
  92. if (totalSticks % 2 != 0)
  93. {
  94. int split = 3;
  95. totalSticks -= 3;
  96. int numOfOnes = totalSticks / 2;
  97. ull int mul = 1;
  98.  
  99. while (numOfOnes--)
  100. {
  101. maxNumber += mul;
  102. mul *= 10;
  103. }
  104. maxNumber += 7 * mul;
  105.  
  106. cout << maxNumber << endl;
  107. }
  108. else
  109. {
  110. int numOfOnes = totalSticks / 2;
  111. ull int mul = 1;
  112. while (numOfOnes--)
  113. {
  114. maxNumber += mul;
  115. mul *= 10;
  116. }
  117. cout << maxNumber << endl;
  118. }
  119. }
  120.  
  121. return 0;
  122. }
  123.  
  124. //APPROACHING A QUESTION
  125. //+ Think of binary search (max of min etc also if n<=2*10^5)
  126. //+ Think of common dp states (Even if it appears as maths but constraints are small)
  127. //+ Check constraints
  128. //+ Keep calm and enjoy the question
  129. //+ Be sure to remove MOD from binpow (if needed)
  130. //+ Try bidirectional analysis for constructive questions
  131. //+ If given some sequence try thinking of prefix sums
  132. //+ If constraints are too large maybe its simple maths
  133. //+ In questions with binary operations think of bits independently and also the change pattern
  134. //+ If two or more binary operations are given mostly there is a relation between them and an arithmatic operator
Success #stdin #stdout 0s 4364KB
stdin
1
8
stdout
711