2018中国大学生程序设计竞赛-网络选拔赛题解

以下所有AC题解程序来自“仙客传奇”团队。


A. Buy and Resell

AC的C++语言程序:

#include
#include
#include
#include
#include
using namespace std;
const int maxn=1e5+10;
priority_queue< int,vector<int>,greater<int> > buy,sell;
typedef long long ll;
//priority_queue 默认从大到小  修改后从小到大 
ll val[maxn];
int main()
{
	int n,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=0;i<n;i++)
	 	scanf("%lld",&val[i]);
		ll ans=0,cnt=0;//tiaixao????
		for(int i=0;i<n;i++){
			//cout<
			if(buy.size()!=0&&sell.size()!=0&&buy.top()<sell.top()&&sell.top()<val[i]){
				ans+=val[i];cnt++;
				sell.push(val[i]);
				buy.pop();
			}
			else if(sell.size()!=0&&sell.top()<val[i]){//交换 
				buy.push(sell.top());
				sell.pop();
				sell.push(val[i]);
				ans+=val[i]-2*buy.top();			
			}
			else if(buy.size()!=0&&buy.top()<val[i]) {
				ans+=val[i];cnt++;//cout<
				sell.push(val[i]);
				buy.pop();
			}
			else buy.push(val[i]),ans-=val[i];
	 	}
	 	while(buy.size()!=0){
	 		ans+=buy.top();
	 		buy.pop();
		 }
 		printf("%lld %lld\n",ans,cnt*2);
 		
		while(!buy.empty()) buy.pop();	
		while(!sell.empty()) sell.pop();
 	
	 }
	
	return 0;
} 
/*
4
8
1 2 3 4 5 6 7 8
4
1 2 10 9
5
9 5 9 10 5
2
2 1*/



B. Congruence equation

题解链接:
HDU 6439 Congruence equation(莫比乌斯反演)
HDU 6439 2018CCPC网络赛 Congruence equationI(杜教筛 + 莫比乌斯反演 + 伯努利数)



C. Dream

题解链接:
2018中国大学生程序设计竞赛 - 网络选拔赛(hdu6440 Dream)



D. Find Integer

AC的C++语言程序:

#include
#include
using namespace std;
const int maxn = 4e4;

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(NULL);cout.tie(NULL);
	int T,n,a;
	
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&a);
		if(n == 0 || n > 2) 
			printf("-1 -1\n");
		else if(n == 1)
			printf("1 %d\n",a + 1);
		else if(n == 2)
		{
			if(a % 2 == 0)
				printf("%d %d\n",a * a / 4 - 1, a * a / 4 + 1);
			else
				printf("%d %d\n",(a - 1) * (a / 2 + 1),(a - 1) * (a / 2 + 1) + 1);	
		}	
	}
	
	return 0;
}



E. GuGu Convolution

题解链接:
HDU 6442 GuGu Convolution(快速幂)
hdu 6442 - 二项式定理



F. Neko and Inu



G. Neko’s loop

题解链接:
hdu6444(最大子段和+gcd)
hdu 6444 - 最大子段和(单调队列)



H. Search for Answer

题解链接:
HDU 6445(竞赛图 + 网络流)
HDU 6445 2018CCPC网络赛1008 Search for Answer(费用流 + 构图)



I. Tree and Permutation

AC的C++语言程序:

#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5;
const int mod = 1e9 + 7;

ll fac[maxn + 10],n,sum;

struct edge
{
	int u,v;
	ll w;
	edge(){}
	edge(int _u,int _v,ll _w):u(_u),v(_v),w(_w){}
}e[maxn + 10];
vector<edge> son[maxn + 10];
int cnt[maxn + 10],vis[maxn + 10];
ll fa[maxn + 10];

void getfac()
{
	fac[0] = fac[1] = 1;
	for(int i = 2;i <= maxn;++i)
		fac[i] = (fac[i - 1] * i) % mod;
}

int dfs(int no)
{
	int cnt = 1;
	vis[no] = 1;
	for(int i = 0;i < son[no].size();++i)
		if(!vis[son[no][i].v])	
		{
			int temp = dfs(son[no][i].v);
			cnt += temp;
			sum = (sum + ((son[no][i].w * fac[n - 1] * 2ll) % mod * ((n - temp) * temp) % mod)) % mod;
		}
	return cnt;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	getfac();

	while(cin>>n)
	{
//		memset(cnt,0,sizeof(cnt));
		memset(vis,0,sizeof(vis));
		for(int i = 1;i <= n;++i)	son[i].clear();
		for(int i = 1;i < n;++i)
		{
			int u,v,w;
			cin>>u>>v>>w;
			son[u].push_back(edge(u,v,w));
			son[v].push_back(edge(v,u,w));
		}
		
		sum = 0;
		dfs(1);			
		cout<<sum<<endl;
	}
	
	return 0;
}

AC的C++语言程序:

#include 
#include 
#include 
#include 
#define INF 2147483647
#define MEM(x, b) memset(x, b ,sizeof(x))
#define ll long long
using namespace std;
const int MOD  = 1e9 + 7;
const int MAXN = 1e5 + 2;
int n;
vector<pair<int, int> > edge[MAXN];
int num[MAXN];
ll fac[MAXN];
ll ans;
void dfs(int crt, int fa){
	for (int i = 0; i < edge[crt].size(); i++) {
		int u = edge[crt][i].first; 
		if (u == fa) continue;
		dfs(u, crt);
		num[crt] += num[u];
		ans =(ans + 1ll*(num[u]) %MOD * (n - num[u]) % MOD* fac[n - 1] % MOD * 2ll % MOD * edge[crt][i].second % MOD) % MOD;
	}
}

int main() {
	fac[0] = 1;
	for (int i = 1;  i < MAXN; i++) {
		fac[i] = (fac[i - 1] * (long long)i % MOD) % MOD;
	}
	while (~scanf("%d", &n)){
		ans = 0;
		for (int i = 1; i <= n; i++) edge[i].clear();
		//MEM(num, 1);
		for (int i = 1;  i<= n; i++) num[i] = 1; 
		for (int i = 1; i < n; i++){
			int u , v, w;
			scanf("%d%d%d",&u,&v,&w);
			edge[u].push_back(make_pair(v, w));
			edge[v].push_back(make_pair(u, w));
		}
		//cout << "ds" << endl;
		dfs(1, 0);
		printf("%lld\n", ans);
	}
	return 0;
}



J. YJJ’s Salesman

AC的C++语言程序:

#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 1e5 + 2;

struct node{
	int x, y, w;
	bool operator < (node A) {
		if (x == A.x) {
			return y>A.y;
		}else return x <  A.x;
	}
};

node p[MAXN];
int tr[MAXN], a[MAXN];
int n ,tot;
int lowbit(int x){
	return (x & (-x));
}

int query(int l) {
	int maxn = 0;
	for (int i = l; i >=1; i-=lowbit(i)){
		maxn = max(maxn, tr[i]);
	}
	return maxn;
}

void add(int pos , int val) {
	tr[pos]= val;
	for (int i = pos; i <= MAXN; i+= lowbit(i) ){
		tr[i] = max(tr[i], val);
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		memset(tr, 0 ,sizeof(tr));
		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].w);
			a[i] = p[i].y;
		}
		sort(a , a + n);
		tot = unique(a, a + n) - a;
		for (int i = 0; i < n; i++){
			p[i].y = lower_bound(a, a + n, p[i].y) - a + 1;
		}
		sort(p, p + n);
		
		for (int  i = 0; i < n; i++){
			int val = query(p[i].y - 1) + p[i].w;
			add(p[i].y, val);
		}
		printf("%d\n", query(MAXN - 1)) ;
	}
	
}



题解链接:
BEIL 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 Goldbach
ACDIJ 2018中国大学生程序设计竞赛网络选拔赛

你可能感兴趣的:(#,ICPC与CCPC赛题题解)