资格赛,除了最后一题比较有趣以外,其他都比较简单
Saving The Universe Again
贪心
一开始想着尽可能将S往前移,但是想了想觉得这样移动好多次,可能才等于后面的S往前移一位的成果。
所以就变成尽可能地将C往末尾移(末尾一段连续的C就不用考虑了),即,每一次移动减少的伤害最大。
int T,n;
vi a1,a2;
int main()
{
cin>>T;
int cases = 1;
while(T--)
{
cin>>n;
a1.clear();
a2.clear();
for(int i = 0, x; i < n; i++)
{
cin>>x;
if(i&1)
a2.pb(x);
else
a1.pb(x);
}
sort(a1.begin(),a1.end());
sort(a2.begin(),a2.end());
int ans = -1;
for(int i = 0; i < n-1; i++)
{
if(i&1)
{
if(a2[i/2]>a1[i/2+1] && ans==-1)
{
ans = i;
break;
}
}
else
{
if(a1[i/2]>a2[i/2] && ans==-1)
{
ans = i;
break;
}
}
}
if(ans == -1)
printf("Case #%d: OK\n", cases);
else
printf("Case #%d: %d\n", cases, ans);
cases++;
}
return 0;
}
Trouble Sort
想法一:
实现两种排序,比较,复杂度 O(N2) O ( N 2 )
想法二:
因为Trouble Sort仅会在奇数位和奇数位或偶数位和偶数位交换,所以将奇偶分开来排序,按顺序比较大小即可。
int main()
{
speed
int T, cases = 1;
ll D, a[33];
string str;
cin >> T;
while (T--)
{
cin >> D >> str;
int n = str.size();
ll sum = 0;
int c = 0;
ll tot = 0;
for(int i = 0; i < n; i++)
{
a[i] = -1;
if(str[i] == 'C')
c++;
else
{
a[i] = (1ll << c);
sum += a[i];
tot++;
}
}
cout << "Case #" << cases++ << ": ";
if(tot > D)
{
cout << "IMPOSSIBLE" << endl;
}
else
{
int ans = 0;
while(sum > D)
{
for(int i = n-1; i > 0; i--)
{
if (a[i-1] == -1 && a[i] > -1)
{
a[i] >>= 1