AtCoder Beginner Contest 173 A-F

A:水題

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/

int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
	int n;
	cin>>n;
	cout<<(1000-n%1000)%1000<<endl; 
	return 0;
}

B:水題

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
char s[M];
int c[4];

int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int n;
  	cin>>n;
  	for(int i=1;i<=n;i++)
  	{
  		cin>>s;
  		if(s[0]=='A')c[0]++;
  		else if(s[0]=='W')c[1]++;
  		else if(s[0]=='T')c[2]++;
  		else c[3]++;
	  }
	cout<<"AC x "<<c[0]<<endl;
	cout<<"WA x "<<c[1]<<endl;
	cout<<"TLE x "<<c[2]<<endl;
	cout<<"RE x "<<c[3]<<endl;
	return 0;
}

C:二進制暴力枚舉即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
char s[10][10];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int h,w,k;
  	cin>>h>>w>>k;
  	for(int i=0;i<h;i++)cin>>s[i];
  	int ans=0;
  	for(int i=0;i<(1<<h);i++)
  	for(int j=0;j<(1<<w);j++)
  	{
  		int tp=0;
  		for(int a=0;a<h;a++)
  		for(int b=0;b<w;b++)
  		{
  			if(i>>a&1)continue;
  			if(j>>b&1)continue;
  			if(s[a][b]=='#')tp++;
		}
		if(tp==k)ans++;
	}
	cout<<ans<<endl;
	return 0;
}

D:顯然大的先放能得到的價值更大。

除了最大值只能得到一次貢獻外, 其他每個值最多貢獻2次(一左一右)

所以肯定優先取最大的數貢獻。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 2e5+7;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int a[M];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int n;
  	cin>>n;
  	for(int i=1;i<=n;i++)cin>>a[i];
  	sort(a+1,a+1+n);
  	ll ans=a[n];
  	int nm=n-2;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(nm>1)nm-=2,ans+=a[i]*2;
  		else if(nm==1)nm=0,ans+=a[i];
  		else break;
	}
	cout<<ans<<endl;
	return 0;
}

E:

分情況即可:

1:0必須取

2:除了0之外的數全部取光

3:只有負數,且負數個數是奇數

4:否則一定可以構造出正數。正數最大的兩個乘積與負數最小兩個乘積不斷相比較即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 2e5+7;
const int mod =1e9+7;
ll pz[M],pf[M];
ll z[M],f[M];
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
  	int n,k;cin>>n>>k;
  	int nm=0;//0的數量 
	int zm=0,fm=0;
  	for(int i=1;i<=n;i++)
  	{
  		int x;cin>>x;
  		if(x>0)z[++zm]=x;
  		else if(x<0)f[++fm]=-x;
  		else nm++;
	}
  	sort(z+1,z+1+zm);
  	sort(f+1,f+1+fm);
  	if(n-k<nm)
  	{
  		cout<<0<<endl;
  		return 0;
	}
	ll ans=1;
	if(zm+fm==k)
	{
		for(int i=1;i<=zm;i++)ans=ans*z[i]%mod;
		for(int i=1;i<=fm;i++)ans=ans*(-f[i])%mod;
		if(fm&1 && nm)ans=0;
		ans=(ans+mod)%mod;
		cout<<ans<<endl;
		return 0;
	}
	if(zm==0 && k&1)
	{
		for(int i=1;i<=k;i++)ans=ans*(-f[i])%mod;
		if(nm)ans=0;
		ans=(ans+mod)%mod;
		cout<<ans<<endl;
		return 0;
	}
	int q=zm,w=fm;
	int nw=0;
	while(nw<=k)
	{
		if(k-nw>=2)
		{
			if((q>=2 && z[q]*z[q-1]>f[w]*f[w-1])||w<2)
			{
				ans=ans*z[q]%mod;
				ans=ans*z[q-1]%mod;
				q-=2;
			}
			else
			{
				ans=ans*f[w]%mod;
				ans=ans*f[w-1]%mod;
				w-=2;
			}
			nw+=2;
		}
		else if(k-nw==1)
		{
			ans=ans*z[q]%mod,q--;
			nw++;
		}
		else break;
	}
	cout<< ans<<endl;
	return 0;
}

F:假設剛開始n個點,0條邊。則結果很簡單。

考慮每次加入一個邊 u -> v ,(u<v)會使得 所有f(l,r) (l<=u,r>=v)的結果減一 ;

因爲:u,v此時一定不連通(這是一顆樹)。然後連邊使得u所在聯通塊與v所在聯通塊連通。

則在S同時包括u,v時,答案會減一。(聯通塊減一)

綜上:直接枚舉減去貢獻即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 2e5+7;
const int mod =1e9+7;
ll pz[M],pf[M];
ll z[M],f[M];
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
  	int n;
	cin>>n;
	ll ans=0;
	for(int i=1;i<=n;i++)ans+=(ll)i*(n-i+1);
	for(int i=1;i<n;i++)
	{
		ll u,v;
		cin>>u>>v;
		if(u>v)swap(u,v);
		ans-=u*(n-v+1);
	 } 
	 cout<<ans<<endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章