暑假算法训练day9(vp cf)

目录

  • A. Bear and Raspberry
  • B. Bear and Strings
  • C. Bear and Prime Numbers
  • D. Bear and Floodlight

A. Bear and Raspberry

枚举即可,语法题

#include 
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,c;
const int N=110;
int a[N];
void solve()
{
	cin>>n>>c;
	int maxv=-ll_INF;
	int res=0;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<n;i++)
	{
		maxv=max(maxv,a[i]-a[i+1]-c);
	}
	if(maxv<0)cout<<0<<endl;
	else cout<<maxv<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

B. Bear and Strings

组合数学,枚举的时候减去上一个字符串重复的

#include 
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
void solve()
{
	string s;
	cin>>s;
	vector<int>v;
	for(int i=0;i<s.size();i++)
		if(s[i]=='b'&&s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='r')v.pb(i);
	int sum=0;
	int pre=0;
	for(int i=0;i<v.size();i++)
	{
		int l=v[i],r=v[i]+3;
		sum+=(l+1)*(s.size()-r)-pre*(s.size()-r);
		pre=l+1;
	}
	cout<<sum<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

C. Bear and Prime Numbers

类似筛质数的一种做法,水题
不要用map会TLE

#include 
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e7+10;
int a[N];
int cnt[N];
bool st[N];
int mp[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i],mp[a[i]]++;
	for(int i=2;i<=N;i++)
	{
		if(st[i])continue;
		for(int j=i;j<=N;j+=i)
		{
			if(mp[j])cnt[i]+=mp[j];
			st[j]=true;
		}
	}
	for(int i=1;i<=N;i++)cnt[i]+=cnt[i-1];
	cin>>m;
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		x=min(N,x),y=min(y,N);
		cout<<cnt[y]-cnt[x-1]<<endl;
	}
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

D. Bear and Floodlight

状态压缩dp, i i i表示某种特定的状态,1表示灯亮,0表示灯灭,dp[i]表示最长的连续的照亮长度,状态转移方程是dp[((i>>j)&1)]=max(dp[(i>>j)&1],dp[i]+turn(dp[i],j));
turn(dp[i],j)表示从dp[i]开始,第 j j j盏灯可以照多长。
其实更像二进制背包问题
暑假算法训练day9(vp cf)_第1张图片

#include 
using namespace std;
const double pi = acos(-1);
const double eps=1e-10;
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
struct node
{
	double x,y,a;
}p[31];
double dp[1<<20];
int n;
double l,r;
double dist(double x1,double y1,double x2,double y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} 
double func(double len,int now)
{
	double d=dist(len,0,p[now].x,p[now].y);
	double c=acos(fabs(p[now].x-len)/d);
	if(p[now].x<len)c=pi-c;//两种情况,统一锐角
	c=pi-p[now].a-c;//灯照到最右边超过r时,也就围成四边形了,此时的c<0
	if(c<0)
	{
		printf("%.12f\n",r);
		exit(0);
	}
	double s=d*sin(p[now].a)/sin(c);//正弦定理
	return min(r,len+s);
}
void solve()
{
	scanf("%lld%lf%lf",&n,&l,&r);
	r-=l;
	for(int i=0;i<n;i++)
	{
		scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].a);
		p[i].x-=l;
		p[i].a=p[i].a/180.0*pi;//tan sin等函数返回的是弧度制,所以都转换成弧度制
	}
	for(int i=0;i<(1<<n);i++)
	{
		for(int j=0;j<n;j++)
		{
			if(((i>>j)&1)==0)
				dp[i|(1<<j)]=max(dp[i|(1<<j)],func(dp[i],j));
		}
	}
	printf("%.12f\n",dp[(1<<n)-1]);
}
signed main()
{
 //   io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

你可能感兴趣的:(codeforces,C++,算法,算法,c++,数据结构,几何计算,状态压缩)