青岛农业大学第九届ACM程序设计竞赛

Problem A 喆神装书

https://ac.nowcoder.com/acm/contest/906/A

题意:是否能够把所有的书都放在两个背包里。

题解:贪心

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum+=a[i];
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    if(n>=1)ans=a[n-1]+a[n];
    cout<<(ans>=sum?"YES":"NO")<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem B 何时收钱

https://ac.nowcoder.com/acm/contest/906/B

题意:如果今年是闰年,那么收的钱是平年的两倍,为了有更好的规划,她想知道包括今年在内的连续两年的收的钱是多少?

题解:

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
bool check(int x){
    return (x%4==0&&x%100!=0)||x%400==0;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    //scanf("%d",&n);
    scanf("%d%d",&n,&m);
    if(check(n)){
        ans=m+m/2;
    }else{
        if(check(n+1)) ans=m+m*2;
        else ans=m+m;
    }
    cout<<ans<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem C 找出叛徒

https://ac.nowcoder.com/acm/contest/906/C

题意:一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。输出那个只出现了一次的元素。

题解:贪心

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&t),a[t]++;
    for(int i=0;i<=1000;i++)if(a[i]==1)ans=i;
    cout<<ans<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem D 群神疯了

https://ac.nowcoder.com/acm/contest/906/D

题意:
一个整数序列 1 ,2 ,... , n。她必须把它分成两个集合A和B,每个元素恰好属于一个集合,且令| sum(A)- sum(B) | 最小。
| x | 是x的绝对值,sum(A)是集合A的元素之和。

题解:贪心+思维

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    if(n%4==0)ans=0;
    if(n%4==1)ans=1;
    if(n%4==2)ans=1;
    if(n%4==3)ans=0;
    cout<<ans<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem E 最小花费

https://ac.nowcoder.com/acm/contest/906/E

题意:n个城市之间建立 (n - 1)条路把这n个城市连接起来,现已知有建立每条路花费的价值为两个城市的收益之和,把这n个城市连接起来所花费最小值。

题解:贪心

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum+=a[i];
    sort(a+1,a+n+1);
    sum+=a[1]*(n-2);
    cout<<sum<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem F 卑微的Fxx

https://ac.nowcoder.com/acm/contest/906/F

题意:
一个数组,重复k次操作,每次操作都从这个数组中找到最小的不为0的数,输出这个数,然后把这个数组中所有的数都减去这个数,如果数组中所有的数都为0,则输出0。

题解:贪心

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a[N];
char str;
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=k;i++){
        if(a[i]==a[i-1]&&i<=n){
            k++;
            continue;
        }
        printf("%d%c",i>n?0:a[i]-sum," \n"[i==k]);
        sum=a[i];
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem G 迷惑敌人

https://ac.nowcoder.com/acm/contest/906/G

题意:s个士兵,排成一个r行c列,但有两个"洞"的矩形方队,缺失士兵数的所有可能值

题解:数学+思维+枚举

(其中A为缺失士兵数量,x为厚度,当且仅当为整数时成立)

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    for(ll i=1;i<=n/7;i++){
        ll a=(n-6*i*i)/(7*i);
        if((n-6.0*i*i)/(7.0*i)==a&&a>0){
            cout<<"Possible Missing Soldiers = "<<2*(a*a%MOD)%MOD<<endl;
            cnt++;
        }
    }
    if(cnt==0){
        cout<<"No Solution Possible"<<endl;
        return 0;
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

Problem H 出题人说这是最简单的题

https://ac.nowcoder.com/acm/contest/906/H

题意:求s到t的最大流

题解:上下界最大流

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
const int Maxn=205;
const int Maxm=10005;
const int inf=1e9;
int n,m,size=1,s,t,tot,ans,S,T,sum;
int first[Maxn],tmp[Maxn],in[Maxn],out[Maxn],depth[Maxn];
struct shu{int to,next,len,v;};
shu edge[Maxm<<3];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') {f=-1;c=getchar();}
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}

inline void build(int x,int y,int z)
{
	edge[++size].next=first[x];
	first[x]=size;
	edge[size].to=y;
	edge[size].len=z;
}

inline void pre()
{
	T=n+1;
	for(int i=1;i<=n;i++)
	{
	  int num=in[i]-out[i];
	  if(num>0) build(S,i,num),build(i,S,0),sum+=num;
	  if(num<0) build(i,T,abs(num)),build(T,i,0);
	}
}

inline void delet(){for(int i=tot+1;i<=size;i++) edge[i].len=0;}

inline int mn(int x,int y){return x < y ? x : y;}

inline bool bfs(int s,int t)
{
	queue<int>q;    //建议手写
	q.push(s);
	memset(depth,0,sizeof(depth));
	depth[s]=1;
	while(!q.empty())
	{
	  int point=q.front();
	  q.pop();
	  for(int u=first[point];u;u=edge[u].next)
	  {
	  	int to=edge[u].to;
	  	if(edge[u].len && !depth[to])
	  	{
	  	  depth[to]=depth[point]+1;
	  	  q.push(to);
	  	  if(to==t) return 1;
	  	}
	  }
	}
	return 0;
}

inline int dinic(int point,int flow,int t)
{
	if(point==t) return flow;
	int sum=0;
	for(int &u=tmp[point];u&&sum<flow;u=edge[u].next)
	{
	  int to=edge[u].to;
	  if(edge[u].len && depth[to] == depth[point] + 1)
	  {
	  	int minn=dinic(to,mn(flow-sum,edge[u].len),t);
	  	if(!(flow-sum)) {depth[to]=0;break;}
	  	edge[u].len-=minn,edge[u^1].len+=minn,sum+=minn;
	  }
	}
	return sum;
}

int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    //scanf("%d",&n);
    n=get_int(),m=get_int(),s=get_int(),t=get_int();
	for(int i=1;i<=m;i++)
	{
	  int x=get_int(),y=get_int(),low=get_int(),up=get_int();
	  in[y]+=low,out[x]+=low,build(x,y,up-low),build(y,x,0);
	}
	tot=size;
	build(t,s,inf),build(s,t,0);
	pre();
	while(bfs(S,T))
	{
	  memcpy(tmp,first,sizeof(first));
	  ans+=dinic(S,inf,T);
	}
	if(ans!=sum) {cout<<"please go home to sleep\n";return 0;}
	first[S]=first[T]=0,ans=0;
	while(bfs(s,t))
	{
	  memcpy(tmp,first,sizeof(first));
	  ans+=dinic(s,inf,t);
	}
	cout<<ans<<"\n";
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
评论
1
2
分享
牛客网
牛客企业服务