Basic knowledge of Number theory for Odd and Even integer is needed to solve this problem.
- Sum of any number of even integers is even
- Sum of even number of odd integers is even
- Sum of odd number of odd integers is odd
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Basic knowledge of Number theory for Odd and Even integer is needed to solve this problem.
I tried to solve it by bfs by considering every possible states. That's why I got TLE. Greedy was the intended solution for this problem.
My code that got TLE in contest:
string s;
map<string, int> mp;
struct state
{
string in;
int swap;
};
int main(void)
{
//freopen("1.txt", "r", stdin);
//freopen("2.txt", "w", stdout);
char in[20];
int k;
int i,j;
int ind;
while(cin >> in >> k)
{
queue<state> pq;
//cout << in << " " << k << endl;
struct state st, temp;
st.in= in;
ind = 1;
st.swap = 0;
pii pii1 = make_pair(st.in, st.swap);
pq.push(st);
mp[st.in] = ind++;
s = "0000000000000000000";
while(!pq.empty())
{
temp = pq.front();
pq.pop();
//if(temp.swap == k)
//{
if(temp.in > s)
{
s = temp.in;
//continue;
}
//}
int sz = temp.in.size();
st = temp;
if(temp.swap == k)
continue;
forl(i,0,sz-1)
{
//forl(j,i+1,sz)
j = i+1;
//{
//
if(i != j && st.in[j] > st.in[i] && !(i == 0 && st.in[j] == '0'))
{
st = temp;
char c = st.in[i];
st.in[i] = st.in[j];
st.in[j] = c;
st.swap = temp.swap+1;
if(mp[st.in]==0)
{
st.swap = temp.swap+1;
mp[st.in] = ind++;
pq.push(st);
}
}
//}
}
}
cout << s << endl;
mp.clear();
}
return 0;
}
Defining proper state was the root challenge in this problem.
State can be defined by dp[a][isFulfilled]
a is current sum and isFulfilled is true/1 if we made at least one single step with length >= d.
Base Cases are:
if(a == n && isFulfilled == 1)
{
return 1;
}
if(a == n && isFulfilled == 0)
{
return 0;
}
if(a>n)
return 0;
Name |
---|