2024.1.26 寒假训练记录(9)

今天复习了下差分约束,发现用的是SPFA,这个复杂度…害,明天有空整个板子吧,估计也不太用得上了

花了好长时间搞训练赛的题,明天比赛时间刚好把题解写了,明天再学学网络流好了

文章目录

  • CF 1798A Showstopper
  • CF 1798B Three Sevens
  • CF 1798C Candy Store
  • CF 1798D Shocking Arrangement

CF 1798A Showstopper

题目链接

小的换给a大的换给b就行

#include 

using namespace std;

#define int long long
#define INF 0x3f3f3f3f

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;

const int N = 11;

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for (int i = 0; i < n; i ++ ) cin >> a[i];
	for (int i = 0; i < n; i ++ ) cin >> b[i];

	for (int i = 0; i < n; i ++ )
	{
		if (a[i] > b[i]) swap(a[i], b[i]);
	}

	bool flag = true;
	for (int i = 0; i < n - 1; i ++ )
	{
		if (a[n - 1] < a[i]) flag = false;
		if (b[n - 1] < b[i]) flag = false;
		if (!flag) break;
	}
	if (flag) cout << "YES\n";
	else cout << "NO\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

CF 1798B Three Sevens

题目链接

记录每个点的最迟抽奖时间,之后排序判断即可

#include 

using namespace std;

#define int long long
#define INF 0x3f3f3f3f

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;

const int N = 11;

void solve()
{
	int m;
    cin >> m;
    map<int, int> mp;
    for (int i = 1; i <= m; i ++ )
    {
        int n;
        cin >> n;
        for (int j = 0; j < n; j ++ )
        {
            int x; cin >> x;
            mp[x] = i;
        }
    }
    vector<PII> mes;
    for (auto t : mp)
    {
        mes.push_back(make_pair(t.second, t.first));
    }
    sort(mes.begin(), mes.end());
    vector<int> ans;
    if (mes.size() < m)
    {
        cout << -1 << '\n';
        return;
    }
    int cnt = 0;
    for (int i = 1; i <= m; i ++ )
    {
        if (cnt == mes.size())
        {
            cout << -1 << '\n';
            return;
        }
        if (mes[cnt].first == i)
        {
            ans.push_back(mes[cnt].second);
            cnt ++ ;
        }
        else if (mes[cnt].first < i)
        {
            i -- ;
            cnt ++ ;
            continue;
        }
        else
        {
            cout << "-1\n";
            return;
        }
    }
    for (auto t : ans) cout << t << ' ';
    cout << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

CF 1798C Candy Store

题目链接

四题里唯一有点价值的一题

a是b的倍数,b是c的倍数(abc都是数组),如果a的gcd能整除c的lcm,那就存在b且b是二者相除的结果

#include 

using namespace std;

#define int long long
#define INF 0x3f3f3f3f

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;

const int N = 11;
const int mod = 0;

int lcm(int a, int b)
{
	return a * b / __gcd(a, b);
}

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for (int i = 0; i < n; i ++ ) cin >> a[i] >> b[i];
	int ans = 1;
	int tmp1 = 0, tmp2 = 1;
	for (int i = 0; i < n; i ++ )
	{
		if (__gcd(tmp1, a[i] * b[i]) % lcm(tmp2, b[i]) == 0)
		{
			// ans ++ ;
			tmp1 = __gcd(tmp1, a[i] * b[i]);
			tmp2 = lcm(tmp2, b[i]);
		}
		else
		{
			ans ++ ;
			tmp1 = 0, tmp2 = 1;
			tmp1 = __gcd(tmp1, a[i] * b[i]);
			tmp2 = lcm(tmp2, b[i]);
		}
	}
	cout << ans << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

CF 1798D Shocking Arrangement

题目链接

双指针,尽可能让区间和等于0

#include 

using namespace std;

#define int long long
#define INF 0x3f3f3f3f

typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;

const int N = 11;

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i ++ ) cin >> a[i];
	sort(a.begin(), a.end());
	int res = a[n - 1] - a[0];
	if (abs(a[0]) >= res || abs(a[n - 1]) >= res)
	{
		cout << "NO\n";
		return;
	}
	int tmp = 0;
	int i = 0, j = n - 1;
	vector<int> ans;
	while (i <= j)
	{
		if (tmp >= 0)
		{
			tmp += a[i];
			ans.push_back(a[i]);
			i ++ ;
		}
		else
		{
			tmp += a[j];
			ans.push_back(a[j]);
			j -- ;
		}
		if (tmp >= res)
		{
			cout << "NO\n";
			return;
		}
	}
	cout << "YES\n";
	for (auto t : ans) cout << t << ' ';
	cout << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	cin >> t;
	while (t -- )
	{
		solve();
	}
}

你可能感兴趣的:(2024寒假训练记录,算法)