They designed a sticker set consisting of [n] stickers and provided a level of happiness for each sticker. the [i] sticker sends [ai] happiness and Adds to the happiness of the other side.
Each user is allowed to use a maximum of one [m] sticker in a message and one sticker of 0, 1 or more, but cannot use more than [k] in each successive sticker.
Write a program that specifies the maximum amount of happiness that any user can transfer to the other user by designing a sticker set.
input :
The first line contains [3] integers [n],[ m] and [k]. The second line contains n integer with [ai] amount of happiness .
2 <= n <= 50
2 <= m,k <= 2*10^9
output :
Print the maximum amount of happiness produced by the sticker set .
#include <iostream>
usingnamespace std;
int main()
{
int n, m, k , p ,t=0,g=0,s=0,u=0;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> p;
if (p > t)
{
g = t;
}
if (p >= t)
{
t = p;
}
if (t > p && g < p|| i == 1)
{
g = p;
}
}
for (int i = 1; i <= m; i++)
{
u++;
if (u <= k)
{
s += t;
}
if (u == k)
{
s += g;
i++;
u = 0;
}
}
cout << s;
}
and i get this :
Compiled Successfully
Test 1
ACCEPTED
Test 2
Time Limit Exceeded
Test 3
ACCEPTED
Test 4
ACCEPTED
Test 5
WRONG ANSWER
Test 6
WRONG ANSWER
Test 7
WRONG ANSWER
Test 8
Time Limit Exceeded
Test 9
Time Limit Exceeded
Test 10
Time Limit Exceeded
Test 11
Time Limit Exceeded
Test 12
WRONG ANSWER
Test 13
Time Limit Exceeded
Test 14
WRONG ANSWER
Test 15
Time Limit Exceeded
Test 16
Time Limit Exceeded
Test 17
Time Limit Exceeded
Test 18
WRONG ANSWER
i can mess around with it till i solve the time limit but what im stuck at is the wrong answers , i need a better code than mine that solve the wrong answers .
#include <iostream>
usingnamespace std;
int main()
{
int n, m, k, p;
int t = 0, g = 0, s = 0, u = 0,i=1;
cin >> n >> m >> k;
do
{
cin >> p;
i++;
if (p > t)
{
g = t;
}
if (p >= t)
{
t = p;
}
if (t > p && g < p)
{
g = p;
}
} while (i <= n);
for (int i = 1; i <= m; i++)
{
u++;
if (u <= k)
{
s += t;
}if (u == k)
{
i++;
}
if (u == k && i <= m)
{
s += g;
u = 0;
}
}
cout << s;
}
i change it but it didnt make any different in result .
#include <iostream>
usingnamespace std;
int main()
{
int n, m, k, p;
int t = 0, g = 0, s = 0, u = 0, i = 1;
cin >> n >> m >> k;
int bigger = (n > m) ? n : m;
do
{
if (i <= n)
{
cin >> p;
i++;
if (p > t)
{
g = t;
}
if (p >= t)
{
t = p;
}
if (t > p && g < p)
{
g = p;
}
}
if (i <= m)
{
u++;
if (u <= k)
{
s += t;
}if (u == k)
{
i++;
}
if (u == k && i <= m)
{
s += g;
u = 0;
}
}
} while (i <= n);
cout << s;
}
If the second loop relied on the first loop to finish first, then this won't work (which it seems to). Another way would be this:
#include <iostream>
usingnamespace std;
int main()
{
int n, m, k, p;
int t = 0, g = 0, s = 0, u = 0, i = 1;
cin >> n >> m >> k;
do
{
cin >> p;
i++;
if (p >= t)
{
t = p;
if (p > t)
{
g = t;
}
}
elseif (t > p && g < p)
{
g = p;
}
} while (i <= n);
for (int i = 1; i <= m; i++)
{
u++;
if (u <= k)
{
s += t;
if (u == k)
{
i++;
if (u == k && i <= m)
{
s += g;
u = 0;
}
}
}
}
std::cout << s;
}
The second one is tweaked to do less if statements when not needed. But probably won't bring run-time low enough.
#include <iostream>
usingnamespace std;
int main()
{
longint n, m, k, p;
longint t = 0, g = 0, s = 0, u = 0;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> p;
if (p > t)
{
g = t;
}
if (p >= t)
{
t = p;
}
if (t > p && g <= p)
{
g = p;
}
}
for (int i = 1; i <= m; i++)
{
u++;
if (u <= k)
{
s += t;
}if (u == k)
{
i++;
}
if (u == k && i <= m)
{
s += g;
u = 0;
}
}
cout << s;
}
it gives me 44% with one error :
Compiled Successfully
Test 1
ACCEPTED
Test 2
Time Limit Exceeded
Test 3
ACCEPTED
Test 4
ACCEPTED
Test 5
ACCEPTED
Test 6
WRONG ANSWER
Test 7
ACCEPTED
Test 8
Time Limit Exceeded
Test 9
Time Limit Exceeded
Test 10
Time Limit Exceeded
Test 11
Time Limit Exceeded
Test 12
ACCEPTED
Test 13
Time Limit Exceeded
Test 14
ACCEPTED
Test 15
Time Limit Exceeded
Test 16
Time Limit Exceeded
Test 17
Time Limit Exceeded
Test 18
ACCEPTED
if u could somehow solve the time limit i could start working on that error which would be better than being stuck for 3 hrs (i need to send this even the time is running out sure im not getting the whole point but its better than 0)
Sorry. The last program I could at least tell what it was doing when looking at the code. This program is a bit more vague and I don't know what "rules" I could make use of to lessen operation time.
ok just forget about my code and try to write this : (if u have the time and u dont mind ty in adv)
it gives u 3 number in line one like : 6 9 2 (for example)
6 is the number of numbers its going to enter in line 2 [ like : 1 3 3 7 2 4]
9 is the number ure suppose to write [combination of line 2 numbers]
and 2 tells u that u cant use a number (of the given numbers in line 2) more than limited [k] times right after u used them like u cant write (if the n was 3 and the range was 1 5 9 and the k was 2 the limit ) : 1 1 1 or 333 or 555 u have to either write 1 13 . 3 3 1 . 9 9 1 . 9 9 3 . . . .
and u should aim for the highest number like use : 9 9 3 highest number 9 in this case and the second highest 3
in this thread the highest number is 7 and second highest is 4 so i think the combination would be : 7+7+4 +7 +7 +4 +7 +7 +4 = 54 total length is 9 and i didnt use a number more than k times [2 in this case ] right after i wrote [k] of it
#include <iostream>
usingnamespace std;
int main()
{
longint n, m, k, p;
longint t = 0, g = 0, s = 0, u = 0;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> p;
if (p >= t)
{
g = t;
t = p;
}
if (t > p&& g <= p)
{
g = p;
}
}
for (longint j = 1; j <= m; j++)
{
u++;
if (u <= k)
{
s += t;
}if (u == k)
{
j++;
}
if (j > m)
{
break;
}
if (u == k && j <= m)
{
s += g;
u = 0;
}
}
cout << s;
}