上午 vjcon3补题

vj3

优先队列维护贪心

image

取一个数的各位只需要
a/100%10,a/10%10,a%10;

去重

bool(int n)
{
    set<int>st;
    for(int j=0;j<n;j++)
    {
        st.insert(a[i][j]);
        
    }
    if(st.size()!=n)
    return 1;
    return 0;
}

中位数 (思维)

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    int s1=0,s2=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int buf;
        scanf("%d",&buf);
        if(buf<m) s2++;
        else s1++;
    }
    /**
    这样理解吧,记大于等于m的数为ai,小于m的为bj。
    每一个含p个bj的序列,就要有p+1个ai的元素
    统计ai与bj的数,直接做差跟0取最值即可
    **/
    printf("%d\n",max(0,s1-s2));
    return 0;
}

下午

map和pair,vector

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;

// 存每个数的因数对
vector<pair<int, int>> q[maxn];

int main() {
    // 示例数据填充
    q[1].push_back({1, 1});
    q[2].push_back({1, 2});
    q[2].push_back({2, 1});

    // 访问第 i 对元素,示例以 i=2 为例
    int i = 2;  // 这里的 i 是你想要访问的索引

    if (!q[i].empty()) {  // 确保 vector 不为空
        // 访问第一个 pair
        pair<int, int> p = q[i].front();  // 获取第一个 pair

        // 获取第一个和第二个元素
        int first = p.first;
        int second = p.second;

        cout << "First element: " << first << endl;
        cout << "Second element: " << second << endl;

        // 删除第一个 pair
        q[i].erase(q[i].begin());  // 删除第一个 pair
    } else {
        cout << "The vector is empty!" << endl;
    }

    return 0;
}

// 示例操作:访问第二个 pair 元素的第一个和第二个元素
if (q[i].size() > 1) {  // 确保 vector 至少有两个元素
    pair<int, int> p = q[i][1];  // 访问第二个 pair
    int first = p.first;
    int second = p.second;

    cout << "First element of second pair: " << first << endl;
    cout << "Second element of second pair: " << second << endl;
}

// 示例操作:打印所有因数对
for (const auto& p : q[i]) {
    cout << p.first << " " << p.second << endl;
}

// 示例操作:清空整个 vector
q[i].clear();

map

#include <bits/stdc++.h>
using namespace std;

int main() {
    // 创建一个 map<int, int>
    map<int, int> mp;
    mp[1] = 10;
    mp[2] = 20;
    mp[3] = 30;

    // 获取第一个键值对
        if (!mp.empty()) {  // 确保 map 不为空
        auto it = mp.begin();  // 获取指向第一个键值对的迭代器

        int key = it->first;   // 获取第一个键
        int value = it->second; // 获取第一个值
		auto it = mp.begin();  // 获取第一个键值对的迭代器
        int key = (*it).first;   // 访问键
        int value = (*it).second; // 访问值

总结 it->first 或 (*it).first

        cout << "First key: " << key << endl;
        cout << "First value: " << value << endl;
    } else {
        cout << "The map is empty!" << endl;
    }

    return 0;
}
auto it = mp.begin(); // 获取第一个键值对
while (it != mp.end()) {
    cout << "Key: " << it->first << ", Value: " << it->second << endl;
    ++it;  // 移动到下一个键值对
}

mp.erase(1);  // 删除键为 1 的元素
auto it = mp.begin();
mp.erase(it);  // 删除迭代器指向的元素

auto it = mp.find(2);  // 查找键为 2 的元素
if (it != mp.end()) {
    cout << "Found key 2 with value: " << it->second << endl;
} else {
    cout << "Key 2 not found" << endl;
}


运用 求解因数对数量

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+10;
int n,m;
bool st[maxn];
vector<pair<int,int>> a;
int q[maxn];
int la[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		int x;
		cin>>x;
		if(st[x]==1)
		{
			a.push_back({q[x],x/q[x]});
			st[x]=0;
		}


		else
		{
			int f=0;
			for(int j=la[x]+1; j*j<=x; j++)
			{
				la[x]=j;
				if(x%j==0)
				{
					a.push_back({j,x/j});
					if(j!=x/j)
					{
						q[x]=x/j;
						st[x]=1;
					}
					f=1;
					break;
				}

			}


			if(f==0)
			{
				cout<<"NO"<<endl;
				return 0;

			}
		}
	}
	cout<<"YES"<<endl;
	for(auto t:a)
	{
		cout<<t.first<<" "<<t.second<<endl;
	}
}

飞机降落

没看懂呢
image
image

另:
con2约数
另一种做法还是不懂
void solve()
{
int a, b, i;
cin >> a >> b;
int ans = 0;

// 遍历所有可能的因数 i,从 1 到 sqrt(b)
for (i = 1; i * i <= b; i++)
{
    // 找到在区间 [a, b] 内第一个能被 i 整除的数 d
    int d = (a / i) * i;

    // 确保 d 是在 [a, b] 区间内的第一个有效的数
    while (d < a || i * i > d)
    {
        d += i;
    }

    // 对于区间 [d, b] 内的所有 d, d 是 i 的倍数
    for (; d <= b; d += i)
    {
        ans += i;  // i 是 d 的一个因数

        // 如果 i 和 d / i 是不同的因数,将 d / i 也加上
        if (i < d / i)
        {
            ans += d / i;
        }
    }
}

cout << ans << endl;  // 输出区间 [a, b] 内所有数的约数和

}

晚上

posted on 2024-07-20 16:29  Hoshino1  阅读(2)  评论(0编辑  收藏  举报