首页 > 其他分享 >CF933-Div3

CF933-Div3

时间:2024-03-18 18:34:12浏览次数:21  
标签:CF933 int ll cin long pos now Div3

D - Rudolf and the Ball Game
深搜+减枝

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1005;
int T,n,m,x;
bool ans[N];int u[N],v[N],tot;
bool step[N][N];
void dfs(int now,int pos)
{
	if(step[now][pos]==1)
	{
		return;
	}
	step[now][pos]=1;
	if(now>m)
	{
		if(!ans[pos])
		{
			tot++;
		}
		ans[pos]=1;
		return;
	}
	if(v[now]==1)
	{
		if(pos+u[now]>n)
		{
			pos+=u[now]-n;
		}else
		{
			pos+=u[now];
		}
		dfs(now+1,pos);
	}else if(v[now]==2)
	{
		if(pos-u[now]>0)
		{
			pos-=u[now];
		}else
		{
			pos=pos-u[now]+n;
		}
		dfs(now+1,pos);
//		step[now+1][pos]=1;
	}else if(v[now]==3)
	{
		step[now][pos]=1;
		int ps=pos,pr=pos;
		if(ps+u[now]>n)
			ps+=u[now]-n;
		else
			ps+=u[now];
		if(pr-u[now]>0)
			pr-=u[now];
		else
			pr=pr-u[now]+n;	
		if(ps==pr)
		{
			v[now]=1;
			dfs(now+1,ps);
		}else
		{
			v[now]=1;
//			if(ps!=pos)
				dfs(now+1,ps);
			v[now]=2;
//			if(pr!=pos)
				dfs(now+1,pr);	
		}
		v[now]=3;
//		step[now+1][ps]=1;
//		step[now+1][pr]=1;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>x;
		memset(step,0,sizeof(step));
//		memset(ans,0,sizeof(ans));
//		memset(u,0,sizeof(u));
//		memset(v,0,sizeof(v));
		tot=0;
		int a,b;char c;
		for(int i=1;i<=m;i++)
		{
			cin>>u[i];
			cin>>c;
			if(c=='?')v[i]=3;
			else if(c=='0')v[i]=1;//shun
			else v[i]=2;
		}
		dfs(1,x);
//		cout<<"&&&&&&";
		cout<<tot<<endl;
		for(int i=1;i<=n;i++)
		{
			if(ans[i])
			{
				cout<<i<<" ";
			}
			ans[i]=v[i]=u[i]=0;
		}
		cout<<endl;
	}
	return 0;
}

也可用数组模拟

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1005;
int T,n,m,x;
vector <int> a;
int tot;ll k;
void get(int s,int l,int r,int n)
{
	for(int i=l;i<r;i++)
	{
		ll p=a[i]+s+n;
		if(p%n==0)
		{
			a[i]=n;
		}else
		{
			a[i]=p%n;
		}
	}
}
int main()
{
		ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>x;
		a.clear();
		a.push_back(x);
		tot=0;
		int r;char c;
		for(int i=1;i<=m;i++)
		{
			cin>>r>>c;
			
			if(c=='0')
			{
				get(r,0,a.size(),n);
			}else if(c=='1')
			{
				get(-r,0,a.size(),n);
			}else
			{
				int k=a.size();
				for(int j=0;j<k;j++)
				{
					a.push_back(a[j]);
				}
				get(r,0,k,n);
				get(-r,k,a.size(),n);
			}
			sort(a.begin(),a.end());
			a.erase(unique(a.begin(),a.end()),a.end());
		}
		cout<<a.size()<<endl;
		for(int i=0;i<a.size();i++)
		{
			cout<<a[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

E - Rudolf and k Bridges
未优化版超时

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
ll T,n,m,k,d,g[N],f[N],sum[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k>>d;
		int w;
		if(d+2>=m)
		{
			
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=m;j++)
					cin>>w;
			}
			cout<<2*k<<endl;
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)cin>>g[j];
			memset(f,0x3f,sizeof(f));
			f[1]=1;
			for(int j=1;j<=m;j++)
			{
//				if(j-d-2>=0)
					for(int k=1;k<j;k++)
					{
						if(j-k-1<=d)
						{
							f[j]=min(f[j],f[k]+g[j]+1);
						}
					}
//				else f[j]=g[j]+1;
			}
//			for(int i=1;i<=m;i++)cout<<f[i]<<" ";cout<<endl;
			sum[i]=sum[i-1]+f[m];
		}
		ll ans=LONG_LONG_MAX,tot=0;
		for(int i=1;i+k-1<=n;i++)
		{
			ans=min(ans,sum[i+k-1]-sum[i-1]);
		}		
		cout<<ans<<endl;
	}
	return 0;
}
单调队列优化
点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
ll T,n,m,k,d,g[N],f[N],sum[N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k>>d;
		int w;
		if(d+2>=m)
		{
			
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=m;j++)
					cin>>w;
			}
			cout<<2*k<<endl;
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			deque <int> q;
			q.push_back(1);
			memset(f,0x7f,sizeof(f));
			f[1]=1;
			cin>>g[1];
			for(int j=2;j<=m;j++)
			{
				cin>>g[j];
				while(!q.empty()&&q.front()<j-d-1)
				{
					q.pop_front();
				}
				f[j]=f[q.front()]+g[j]+1;
				while(!q.empty()&&f[j]<=f[q.back()])
				{
					q.pop_back();
				}
				q.push_back(j);
			}
			sum[i]=sum[i-1]+f[m];
		}
		ll ans=LONG_LONG_MAX,tot=0;
		for(int i=1;i+k-1<=n;i++)
		{
			ans=min(ans,sum[i+k-1]-sum[i-1]);
		}		
		cout<<ans<<endl;
	}
	return 0;
}

可以用muliset,操作不当容易Re

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
int T,n,m,k,d;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k>>d;
		vector <ll> a(n+1);
		for(int i=1;i<=n;i++)
		{
			vector <int> v(m+1);
			vector <ll> dp(m+1,1e9);
			multiset <ll> mst={1};
			dp[0]=1;
			cin>>v[0];
			for(int j=1;j<m-1;j++)
			{
				cin>>v[j];
				dp[j]=*mst.begin()+v[j]+1;
				if(j-d-1>=0)
				{
					mst.erase(mst.find(dp[j-d-1]));
				}
				mst.insert(dp[j]);
			}
			cin>>v.back();
			dp.back()=*mst.begin()+1;
			a[i]=dp.back();	
		}
		ll ans=0;
		for(int i=1;i<=k;i++)
		{
			ans+=a[i];
		}
		ll tot=ans;
		for(int i=k+1;i<=n;i++)
		{
			ans=ans-a[i-k]+a[i];
			tot=min(ans,tot);
		}
		cout<<tot<<endl;
//		f[1][0]=0;
	}
	return 0;
}

F - Rudolf and Imbalance
这题需要找到最大值和次大值,然后尝试减小最大值,最后减小的最大值再与次大值比较

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
int T,n,m,k;ll a[N];
struct node
{
	ll l,r,id,cha;
	bool operator < (const node &A)const
	{
		return cha<A.cha;
	}
};
set <ll> f,d;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>k;
		int l,r;ll cha=0;
		memset(a,0,sizeof(a));
		f.clear();d.clear();
		ll data=0,dataa=0,lb=0,rb=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(i>1)
			if(data<a[i]-a[i-1])
			{
				dataa=data;
				data=a[i]-a[i-1];
				lb=a[i-1];rb=a[i];
			}else if(dataa<a[i]-a[i-1])
			{
				dataa=a[i]-a[i-1];
			}
		}
		ll dd,ff;
		for(int i=1;i<=m;i++)
		{
			cin>>dd;
			d.insert(dd);
		}
		for(int i=1;i<=k;i++)
		{
			cin>>ff;
			f.insert(ff);
		}
		bool p=0;
		ll ans=data;
		for(auto i:f)
		{
			auto dh=d.lower_bound((lb+rb)/2-i);
			ll dh1=*dh;
			ans=min(ans,max(dh1+i-lb,rb-i-dh1));
			if(dh!=d.begin())
			{
				dh--;
				dh1=*dh;
				ans=min(ans,max(dh1+i-lb,rb-i-dh1));
			}
		}
		cout<<max(dataa,ans)<<endl;
	}
	return 0;
}
/*
3
7 6 5
1 4 7 10 18 21 22
2 3 5 7 4 2
6 8 9 3 2
*/

G - Rudolf and Subway

卡的很死,数组内存开大了回超时
分层图最短路/建虚点
可用01BFS优化

点击查看代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
int n,m,T;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		int u,v,w;
		map <int,int> colr;
		vector <vector<pair<int,int> > > g(n+1);
		int idx=n+1;
		colr.clear();
		for(int i=1;i<=m;i++)
		{
			cin>>u>>v>>w;
			g[u].push_back({v,w});
			g[v].push_back({u,w});
			if(!colr[w])
			{
				colr[w]=idx;
				idx++;
			}
		}
		int s,e;
		cin>>s>>e;
		if(s==e)
		{
			cout<<0<<endl;
			continue;
		}
		vector<set<int>> bg(n + colr.size() + 3);
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<g[i].size();j++)
			{
				int to=g[i][j].first;
				int c=g[i][j].second;
				int id=colr[c];
				bg[i].insert(id);
				bg[id].insert(i);
				bg[to].insert(id);
				bg[id].insert(to);		
			}
		}
		vector<int> used(bg.size());
		vector<int> d(bg.size(), -1);
		queue <int> q;
		q.push(s);
		used[s]=1;
		d[s]=0;
		while(!q.empty())
		{
			int u=q.front();q.pop();
			for(auto to:bg[u])
			{
				if(!used[to])
				{
					d[to]=d[u]+1;
					used[to]=1;
					q.push(to);
				}
			}
		}
		if(d[e]>0)cout<<d[e]/2<<endl;
		else cout<<-1<<endl;
	}
	return 0;
}

标签:CF933,int,ll,cin,long,pos,now,Div3
From: https://www.cnblogs.com/wlesq/p/18081126

相关文章

  • CF933-Div3 大致思路+题解
    A-RudolfandtheTicket纯水题暴力枚举直接过$code$#include<bits/stdc++.h>#definefo(x,y,z)for(int(x)=(y);(x)<=(z);(x)++)#definefu(x,y,z)for(int(x)=(y);(x)>=(z);(x)--)inlineintqr(){ charch=getchar();intx=0,f=1; for(;ch<'0......
  • cfRound933div3-E题解
    E-RudolfandkBridges题意:选择的桥在连续的行中,每个桥的支架安装位置是可以不一样的.做法:赛时也感觉也感觉是dp,但是害怕dp,就选择了逃避.往贪心方向想,认为每次到了每个跳板都要跳到最远距离,实际上这样是不行的.很明显,可能存在近一点的点花费更少。实际上是dp,而且也不......
  • div3笔记
     Problem-E-Codeforces这道题用了记录一个数末尾零的板子(敲重点)!!!再说一遍,简单博弈论就是贪心!1voidsolve(){2cin>>n>>m;3vector<int>a(n),b(n);4for(inti=0;i<n;i++)cin>>a[i];5intlen=0;//这组数字总共有几位,总长度6......
  • Codeforces 920 (div3)
    Problem-A-Codeforces没什么问题,几个ifelse语句,判断一下条件;#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;intmain(){intk;cin>>k;while(k--){intx1,y1,x2,y2,x3,y3,x4,y4;......
  • codeforces刷题(1100):1862C_div3
    C、FlowerCityFence跳转原题点击此:该题地址1、题目大意  给你n块长度依次不递增的紧密连接在一起的垂直木板,将它们水平横过来,问其组成的全新n块木板的长度是否与原来的木板长度一致。  注意:这里的长度是指:木板的高度。水平摆放后的木板是左对齐,所以其长度就是各个木板水......
  • codeforces刷题(1100):1907C_div3
    C、RemovalofUnattractivePairs跳转原题点击此:该题地址1、题目大意  给定一个字符串,可以删除相邻的两个不相等的字符。问你删除后能得到最小的字符串长度为多少。2、题目解析  因为只要两个不相等的字符相邻就能消除,所以只需要找到数量最多的字符,只要它的数量比其它字......
  • Codeforce Round 916(div3)
    CodeforcesRound916(div3)[Problem-A-Codeforces]:ProblemsolvingLogA.题直接看样例进行分析,发现每一次出现的字符代表着用了1分钟来看这道题,每道题都有固定的解题时间,只要达到了这个解题时间,就可以将这题解出来,答案就要加上1;同时要注意将解决过的问题要标记一下;#in......
  • Codeforces Round 891 (Div3)
    CodeforcesRound891(Div.3)A.ArrayColoring这个我vp的时候写复杂了,想不到答案的思路这么清晰,将两部分分别看,将偶数加进去其奇偶性不变,只有奇数加进去才会改变奇偶性,so只有改变偶数次奇偶性才能使其奇偶性相同,所以cnt%2==0.#include<bits/stdc++.h>usingnamespacestd;......
  • CF909 div3
    CF909div3A.GamewithIntegers题意两人博弈,给出一个数字,每人每次可以选择令该数字+1或者-1。如果在10步以内可以令数字为3的倍数,先手胜。否则后手胜。数据范围多组数据,\(1<=T<=100,1<=n<=1000\)题解后手可以恢复现场,所以先手最多只能有效操作1次。若+1或者-1......
  • TheForces Round #24 (DIV3-Forces)11.1
    TheForcesRound#24(DIV3-Forces)A-BanisandCards思路:不大于n的m的倍数的和#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128#definedoublelongdoubletypedefpair<int,int>PII;typedefpair<string,int>......