伞兵代码 不吝赐教
1539:生成排列
题解:
记得开longlong就行。
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
typedef long long ll;
int a[100005];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n);
ll cnt = 0;
for (int i = 0; i < n; i++)
{
cnt += abs(a[i] - (i + 1));
}
cout << cnt << endl;
}
}
1541:XCPC
题解:
无
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
string s;
cin >> s;
int i = 0, c = 0, p = 0;
for (int j = 0; j < s.length(); j++)
{
if (s[j] == 'c' || s[j] == 'C') { c++; }
else if (s[j] == 'i' || s[j] == 'I') { i++; }
else if (s[j] == 'p' || s[j] == 'P') { p++; }
}
int cnt = 0;
while (i&& c >= 2&& p)
{
i--, c -= 2, p--;
cnt++;
}
while (c >= 3&&p)
{
c -= 3, p--;
cnt++;
}
cout << cnt << endl;
}
}
1542:中位数-2
题解:
无
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
multiset<int>t;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
t.insert(x);
}
int i = 1;
multiset<int>::iterator it = t.begin();
for (; it != t.end(); ++it, ++i)
{
if (t.size() % 2 == 0)
{
if (i == t.size() / 2)break;
}
else
{
if (i == (t.size() + 1) / 2)break;
}
}
while (m--)
{
string s;
cin >> s;
if (s == "Query")
{
if (t.size() % 2 == 0)
{
int i = *it;
it++;
int p = *it;
it--;
cout << (i + p) / 2 << endl;
}
else
{
cout << *it << endl;
}
}
else if (s == "New")
{
int x;
cin >> x;
t.insert(x);
if (t.size() % 2 == 0)
{
if (x < * it)it--;
}
else
{
if (x > *it)it++;
}
}
}
}
1543:取石子游戏
题解:
看代码
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, A, B, C, a, b, c;
cin >> n >> A >> B >> C >> a >> b >> c;
while (n--)
{
int a_, b_, c_;
if (A < c) { c_ = A; }
else c_ = c;
if (B < a) { a_ = B; }
else a_ = a;
if (C < b) { b_ = C; }
else b_ = b;
A = A + a_ - c_;
B = B + b_ - a_;
C = C + c_ - b_;
}
cout << A << " " << B << " " << C << endl;
}
}
1544:签到题-6
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int x[10005];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
x[a]++;
}
for (int i = 1; i <= 10000; i++)
{
if (x[i] == 2)
{
cout << i << endl;
break;
}
}
}
1545:炉石传说-2
题解:
由题意得钱不会有剩余,考虑怎么获得最多的钱就行,而唯一能获得钱的方法就是出售随从,问题即转化为怎么出售最多的随从即可,每出售一个随从实际的总钱数加一,最后用实际的总钱数除以三即可。
代码:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n >> x;
ll real_n = n;
int t_x = x;
while (n)
{
int p;
while (min(n / 3, x) != 0 && x != 0)
{
p = min(n / 3, x);
n -= 2 * p;
real_n += p;
x -= p;
}
n--;
x = t_x;
}
cout << real_n / 3 << endl;
}
}