Educational Codeforces Round 108 (Rated for Div. 2)——vector用法以及不超时解法

29 篇文章 0 订阅

C. Berland Regional
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp is an organizer of a Berland ICPC regional event. There are n universities in Berland numbered from 1 to n. Polycarp knows all competitive programmers in the region. There are n students: the i-th student is enrolled at a university ui and has a programming skill si.

Polycarp has to decide on the rules now. In particular, the number of members in the team.

Polycarp knows that if he chooses the size of the team to be some integer k, each university will send their k strongest (with the highest programming skill s) students in the first team, the next k strongest students in the second team and so on. If there are fewer than k students left, then the team can’t be formed. Note that there might be universities that send zero teams.

The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is 0.

Help Polycarp to find the strength of the region for each choice of k from 1 to n.

Input
The first line contains a single integer t (1≤t≤1000) — the number of testcases.

The first line of each testcase contains a single integer n (1≤n≤2⋅105) — the number of universities and the number of students.

The second line of each testcase contains n integers u1,u2,…,un (1≤ui≤n) — the university the i-th student is enrolled at.

The third line of each testcase contains n integers s1,s2,…,sn (1≤si≤109) — the programming skill of the i-th student.

The sum of n over all testcases doesn’t exceed 2⋅105.

Output
For each testcase print n integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size k.

Example
inputCopy
4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083
outputCopy
29 28 26 19 0 0 0
24907 20705 22805 9514 0 0 0 0 0 0
43 43 43 32 38 43
3083
Note
In the first testcase the teams from each university for each k are:

k=1:
university 1: [6],[5],[5],[3];
university 2: [8],[1],[1];
k=2:
university 1: [6,5],[5,3];
university 2: [8,1];
k=3:
university 1: [6,5,5];
university 2: [8,1,1];
k=4:
university 1: [6,5,5,3];

1。总是写不出Div2C题,以后狂刷历来Div2C题一段时间

2。https://www.cnblogs.com/YJthua-china/p/6550960.html
——vector数组操作

1.声明及初始化


vector<int> a;    //声明一个int型向量a
vector<int> a(10);    //声明一个初始大小为10的向量
vector<int> a(10, 1);    //声明一个初始大小为10且初始值都为1的向量
 
vector<int> b(a);    //声明并用向量a初始化向量b
vector<int> b(a.begin(), a.begin()+3);    //将a向量中从第0个到第2个(共3个)作为向量b的初始值   
 
int n[] = {1, 2, 3, 4, 5};
vector<int> a(n, n+5);    //将数组n的前5个元素作为向量a的初值
vector<int> a(&n[1], &n[4]);    //将n[1] - n[4]范围内的元素作为向量a的初值
 2.添加元素

vector<int> a;
a.push_back(1);    //在尾部加入一个数据
a.push_back(2);
a.pop_back();      //删除最后一个数据
a.insert(a.begin(), 0);    //在a.begin()之前加入0
a.erase(a.begin());    //将a.begin()的元素删除
a.erase(a.begin() + 1, a.end());    //将第二个元素以后的元素均删除
 3.判断vector是否为空

vector<int> a;
if(a.empty()){
    a.push_back(1);
}
 4.遍历访问vector


vector<int> a;
//像数组一样以下标访问 
for(int i = 0; i < a.size(); i++){
    cout << a[i];
}
 
//以迭代器访问
vector<int>::iterator it;
for(it=a.begin(); it!=a.end(); it++){
    cout<<*it<<" " ;
}
 5.排序必须包含algorithm头文件

#include <algorithm>
 
vector<int> a;
sort(a.begin(), a.end());
6.以vector实现二维数组

vector<vector<int>> a(10, vector<int>(5));    //创建一个10行5列的int型二维数组 相当于a[10][5];
 同理可以创建三维及以上数组,这些多维数组的操作同一维的vector类似。

DevC++调了很久,因为它调不了STL总是卡
应该用vs2019,下次要用这个

3。这个题目里
超时代码和不超时代码的区别,在于利用了sum的互不相干性
所以我们不要k为1~n时查看数组各一次
而是查看一次数组,对于每条,加在sum[k]上
好处就是节省了时间——对于每条,只要看到它的大小就可以了
4。为什么比赛的时候想不到:
沉着冷静,愿坐冷板凳
想清楚,
1.分析好了时间复杂度再写
——这里,二重循环交换次序可能可以减少次数,下次可以从这想
2.分析好int还是long long 防止WA

#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
#endif
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define INF 0x3f3f3f3f
#define fastio 	std::ios::sync_with_stdio(false);std::cin.tie(0);
#define N 200005
struct Student{
	ll a=0;
	ll s=0;
	bool operator < (Student& y){
		return a>y.a;//为了排序 
	}
};
vector<Student>u[N];

int n,maxn=0;
int su[N];
int vis[N];
ll sum[N];
int main(){
	fastio
	int t;
	cin>>t;
	int num=0;
	while(t--){
		cin>>n;
		mem(vis,0);
		mem(sum,0);
		_forplus(i,1,n){
			vector<Student>().swap(u[i]);
		}
		_forplus(i,1,n){
			cin>>su[i];
			vis[su[i]]+=1;
		}
		maxn=0;
		_forplus(i,1,n){
			if(maxn<vis[su[i]]){
				maxn=vis[su[i]];
			}
		}
		Student st;
		_forplus(i,1,n){
			cin>>st.a;
			u[su[i]].pb(st);
		}
		_forplus(i,1,n){
			if(!vis[i])continue;
			sort(u[i].begin(),u[i].end());
			u[i][0].s=u[i][0].a;
			forplus(j,1,u[i].size()){
				u[i][j].s=u[i][j-1].s+u[i][j].a;
			}
			/*{
				forplus(j,0,u[i].size()){
					cout<<u[i][j].a<<' ';
				}cout<<"——"<<i<<endl;
				forplus(j,0,u[i].size()){
					cout<<u[i][j].s<<' ';
				}cout<<"——s of "<<i<<endl;
			}*/
		}
		_forplus(i,1,n){
			_forplus(j,1,u[i].size()){//把人数放内层 ,优化的是个数 
				sum[j]+=u[i][u[i].size()/(j)*(j)-1].s;
			}
		}
		_forplus(j,1,n){
			cout<<sum[j]<<' ';
		}
		cout<<endl;
		{
		//	cout<<"***"<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值