A很水,B题我卡了,丢人,整体思路还差一点,主要是细节没考虑好,然后就开始码代码,然后果然死了,呵呵呵呵。C题很水,一度怀疑BC放反了,D题也好做,但是我刚开始的时候边界多写了一个=,导致部分数据会有dp[-1],导致WA test4,OJ警告index out of bound,我还找了半天,丢人。改了之后WA test7,当场自闭。后来把标程交了,发现思路一样啊,虽然标程更简洁。然后发现我把a的数据范围1e9看成n的2e5了,打扰了。
E题受益匪浅,树的构造,学到了学到了,嗯,不会写,上标程+注释。
F题一直以为是哈希,然后发现标程直接dp搜最长然后暴力,是我输了。
A
#include
#include
#include
using namespace std;
int main()
{
int n;cin>>n;
vector<int>f(101);
for(int i=0;i<n;i++){
int x; cin>>x;
f[x]++;
}
sort(f.begin(),f.end());
reverse(f.begin(),f.end());
cout<<f[0]<<endl;
return 0;
}
B
直接标程了,我的WA了。
#include
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int a, b, x;
cin >> a >> b >> x;
if (x % 2 == 0) {
if (a > b) {
for (int i = 0; i < x / 2; ++i)
cout << "01";
cout << string(b - x / 2, '1');
cout << string(a - x / 2, '0');
} else {
for (int i = 0; i < x / 2; ++i)
cout << "10";
cout << string(a - x / 2, '0');
cout << string(b - x / 2, '1');
}
} else if (a > b) {
for (int i = 0; i < x / 2; ++i)
cout << "01";
cout << string(a - x / 2, '0');
cout << string(b - x / 2, '1');
} else {
for (int i = 0; i < x / 2; ++i)
cout << "10";
cout << string(b - x / 2, '1');
cout << string(a - x / 2, '0');
}
cout << endl;
return 0;
}
C
#include
#include
using namespace std;
double sum[5005];
int main()
{
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++){
double x; cin>>x;
sum[i]=sum[i-1]+x;
}
double res=0;
for(int i=1;i<=n;i++){
for(int j=i+m-1;j<=n;j++){
res=max(res,(sum[j]-sum[i-1])/(j-i+1));
}
}
printf("%.10f",res);
return 0;
}
D
标程的很简洁,我的写复杂了。
#include
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, q;
cin >> n >> q;
vector<int> cnt(31);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
++cnt[__builtin_ctz(x)];
}
while (q--) {
int x;
cin >> x;
int ans = 0;
for (int i = 30; i >= 0 && x > 0; --i) {
int need = min(x >> i, cnt[i]);
ans += need;
x -= (1 << i) * need;
}
if (x > 0)
ans = -1;
cout << ans << endl;
}
return 0;
}
E
#include
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, d, k;
cin >> n >> d >> k;
if (d >= n) {
cout << "NO" << endl;
return 0;
}
vector<int> deg(n);//存每一个点的度数
vector<pair<int, int>> ans;//存边
set<pair<int, int>> q;
for (int i = 0; i < d; ++i) {//取前d个点做一条主链
++deg[i];
++deg[i + 1];
if (deg[i] > k || deg[i + 1] > k) {
cout << "NO" << endl;
return 0;
}
ans.push_back(make_pair(i, i + 1));
}
for (int i = 1; i < d; ++i)
q.insert(make_pair(max(i, d - i), i));
for (int i = d + 1; i < n; ++i) {
while (!q.empty() && deg[q.begin()->second] == k)
q.erase(q.begin());
if (q.empty() || q.begin()->first == d) {
cout << "NO" << endl;
return 0;
}
++deg[i];
++deg[q.begin()->second];
ans.push_back(make_pair(i, q.begin()->second));
q.insert(make_pair(q.begin()->first + 1, i));
}
assert(int(ans.size()) == n - 1);
cout << "YES" << endl;
vector<int> p(n);
for (int i = 0; i < n; i++)
p[i] = i;
random_shuffle(p.begin(), p.end());
for (int i = 0; i < n - 1; ++i)
if (rand() % 2)
cout << p[ans[i].first] + 1 << " " << p[ans[i].second] + 1 << endl;
else
cout << p[ans[i].second] + 1 << " " << p[ans[i].first] + 1 << endl;
return 0;
}
F
#include
using namespace std;
const int N = 303;
int n;
bool eq[N][N];
int dp[N][N];
string s[N];
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n;
int allsum = n - 1;//空格的长度
for (int i = 0; i < n; ++i) {
cin >> s[i];
allsum += s[i].size();
}
for (int i = 0; i < n; ++i) {
eq[i][i] = true;
for (int j = 0; j < i; ++j) {
eq[i][j] = eq[j][i] = s[i] == s[j];
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
if (eq[i][j]) {
if (i + 1 < n && j + 1 < n)
dp[i][j] = dp[i + 1][j + 1] + 1;
else
dp[i][j] = 1;
}
}
}
int ans = allsum;
for (int i = 0; i < n; ++i) {//替换的第一部分的起点为 i
int sum = 0;
for (int j = 0; i + j < n; ++j) {//替换单词i后的j 个单词
sum += s[i + j].size();// i到i+j 共j+1个单词的总长度
int cnt = 1;
for (int pos = i + j + 1; pos < n; ++pos) {//替换的第二部分的起点pos
if (dp[i][pos] > j) {
++cnt;//记录可替换的个数
pos += j;
}
}
int cur = allsum - (sum+j) * cnt + (j + 1) * cnt;//自己推一下,挺好想的公式
if (cnt > 1 && ans > cur) {
ans = cur;
}
}
}
cout << ans << endl;
return 0;
}