C题
比较可惜吧。思路有了,写法比较复杂。
一个是自己对于除0外,其余都是True,掌握不好。
然后把这个改了,发现tle了,发现memset太慢了。也不是memset太慢了。我mem的数字太大了,1e6.。
按下面的这样写就很快了。
然后改成
for(int i=1;i<=2*n;i++)
hsh[a[i]]=0;
就神奇的AC了,而且跑的很快
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 3;
typedef long long ll;
typedef pair<int, int> PII;
ll hsh[N];
const int maxn = 2005;
ll a[maxn];
ll n;
bool judge(int i)
{
ll res = a[n * 2];
bool j = false;
hsh[a[i]]--;
hsh[a[2 * n]]--;
for (int x = 2 * n - 1; x >= 1; x--)
{
if (hsh[a[x]])
{
if (hsh[res - a[x]])
{
hsh[res - a[x]]--;
hsh[a[x]]--;
if(hsh[a[x]]<0||hsh[res-a[x]]<0)
{
j=true;
break;
}
res = a[x];
}
else
{
j = true;
break;
}
}
}
if (!j)
return true;
else
return false;
}
void solve()
{
cin >> n;
for (int i = 1; i <= 2 * n; i++)
{
cin >> a[i];
hsh[a[i]]++;
}
sort(a + 1, a + 1 + 2 * n);
ll flag = 0;
ll x;
for (int i = 1; i < 2 * n; i++)
{
for(int i=1;i<=2*n;i++)
hsh[a[i]]=0;
for (int i = 1; i <= 2 * n; i++)
{
hsh[a[i]]++;
}
if (judge(i))
{
flag = a[n * 2] + a[i];
x = i;
break;
}
}
//cout<<flag<<" "<<x<<endl;
if (!flag)
cout << "NO" << endl;
else
{
cout << "YES" << endl;
cout << flag << endl;
cout << a[n * 2] << " " << a[x] << endl;
for(int i=1;i<=2*n;i++)
hsh[a[i]]=0;
for (int i = 1; i <= 2 * n; i++)
{
hsh[a[i]]++;
}
hsh[a[2 * n]]--;
hsh[a[x]]--;
flag = a[2 * n];
for (int i = 2 * n - 1; i >= 1; i--)
{
if (hsh[a[i]])
{
hsh[flag - a[i]]--;
hsh[a[i]]--;
cout << a[i] << " " << flag - a[i] << endl;
flag = a[i];
}
}
}
for(int i=1;i<=2*n;i++)
hsh[a[i]]=0;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
solve();
}
}
D题
似乎是个模拟思维题
待补。
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <map>
#include <set>
#define int long long int
#define float long double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define fi first
#define se second
using namespace std;
int mod = 1000000000 + 7;
const int N = 2e3 + 5;
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int a[n + 1];
for (int i = 1; i <= n; i++)
cin >> a[i];
int l[n + 1], r[n + 2];
l[0] = 0, r[n + 1] = 0;
for (int i = 1; i <= n; i++)
{
if (l[i - 1] == -1)
l[i] = -1;
else if (a[i] < l[i - 1])
l[i] = -1;
else
l[i] = a[i] - l[i - 1];
}
for (int i = n; i >= 1; i--)
{
if (r[i + 1] == -1)
r[i] = -1;
else if (a[i] < r[i + 1])
r[i] = -1;
else
r[i] = a[i] - r[i + 1];
}
int flag = 0;
for (int i = 0; i <= n; i++)
{
if (l[i] == r[i + 1] && l[i] != -1)
flag = 1;
}
for (int i = 1; i < n; i++)
{
if (l[i - 1] != -1 && r[i + 2] != -1 && a[i + 1] >= l[i - 1] && a[i] >= r[i + 2] && l[i - 1] + a[i] == a[i + 1] + r[i + 2])
flag = 1;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}