思路:因为数据很水,我们可以直接硬搜,bfs的每个节点里记录一下每面墙是否被破坏.每次移动判断一下我是否要打破墙即可.
- #include
- #include
- #include
- #include
- using namespace std;
- typedef vector<int> vec;
- int n,m,k,sx,sy,tx,ty;
- int dx[] = {1,0,-1,0};
- int dy[] = {0,1,0,-1};
- struct node
- {
- int s,x,y;
- int w[16];
- void init(){
- for(int i=1;i<=k;i++)w[i] = 0;
- }
- bool operator < (const node &A)const{
- return s > A.s;
- }
- };
- struct edge
- {
- int x1,y1,x2,y2;
- }e[20];
- bool st[20][20];
- priority_queue
q; - void bfs()
- {
- while(!q.empty())q.pop();
-
- node sta = {0,sx,sy};
- sta.init();
- q.push(sta);
- while(!q.empty())
- {
- node p = q.top();
- q.pop();
- int x = p.x, y = p.y;
- if(st[x][y]) continue ;
-
- // printf("x = %d y = %d p.s = %d\n",x,y,p.s);
-
- if(x == tx && y == ty)
- {
- printf("%d\n",p.s);
- break;
- }
-
- st[x][y] = 1;
- for(int i=0;i<4;i++)
- {
- int r = x + dx[i], c = y + dy[i];
- if(r < 0 || r >= n || c < 0 || c >= m || st[r][c]) continue ;
- // printf("r = %d c = %d\n",r,c);
- node nex = p;
- int x1,y1,x2,y2;
- if(i == 0)//x + 1
- {
- x1 = x2 = r;
- y1 = y, y2 = y + 1;
- }
- else if(i == 1)//y + 1
- {
- y1 = y2 = c;
- x1 = x, x2 = x + 1;
- }
- else if(i == 2)//x - 1
- {
- x1 = x2 = x;
- y1 = y, y2 = y + 1;
- }
- else//y - 1
- {
- y1 = y2 = y;
- x1 = x,x2 = x + 1;
- }
-
- for(int j=1;j<=k;j++)
- {
- if(p.w[j]) continue ;
- if(x1 < e[j].x1 || x1 > e[j].x2 || x2 < e[j].x1 || x2 > e[j].x2) continue ;
- if(y1 < e[j].y1 || y1 > e[j].y2 || y2 < e[j].y1 || y2 > e[j].y2) continue ;
- nex.s ++;
- nex.w[j] = 1;
- }
- nex.x = r, nex.y = c;
- q.push(nex);
- }
- }
- }
- void init(int id)
- {
- int x1,x2,y1,y2;
- scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
- if(x1 > x2)swap(x1,x2);
- if(y1 > y2)swap(y1,y2);
- e[id] = {x1,y1,x2,y2};
- }
- void solve()
- {
- scanf("%d%d%d",&n,&m,&k);
- scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
-
- for(int i=0;i<=n;i++)
- for(int j=0;j<=m;j++) st[i][j] = 0;
-
- for(int i=1;i<=k;i++) init(i);
- bfs();
- return ;
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)solve();
- return 0;
- }
给定n个物品和体积为m的背包,物品有体积和价值。要求选择若干个物品填满背包,并且使得物品价值的异或和最大。
首先既然是背包dp,就先写出状态,f [ i ][ j ][ k ]表示三层含义,但是由于最大异或和没有最优子结构的性质,就不能作为最后一维的状态,那么i就表示在采用前i中物品的情况下,能够组成异或和为j的体积为k的方法是否存在,然后直接按照01背包,选与不选进行状态转移.但是由于n的3次方不仅宝空间也爆时间,又因为最后一维里面的值只包含0,1,所以想到用bitset来优化(具体用法不细说).bitset的特性可以让这个01数组的空间和时间都除以32.
- #include
- #define int long long
- using namespace std;
- const int N =5e5+10,mod=998244353;
- int v[2000],w[2000];
- bitset<1024>f[2][2000];
- void solve()
- {
- int n,m;
- cin>>n>>m;
- for(int i=1;i<=n;i++)
- cin>>v[i]>>w[i];
- for(int i=0;i<1024;i++)
- f[0][i].reset();
- f[0][0].set(0);
- for(int i=1;i<=n;i++)
- for(int j=0;j<1024;j++)
- f[i&1][j]=(f[(i&1)^1][j])|(f[(i&1)^1][j^w[i]]<
- int ans=0;
- for(int i=1023;i>=0;i--)
- if(f[n&1][i][m])
- {
- cout<"\n";
- return ;
- }
- cout<<"-1\n";
- return ;
- }
- signed main()
- {
- cin.tie(0);
- cout.tie(0);
- ios::sync_with_stdio(0);
- int t;
- cin>>t;
- while(t--)
- solve();
- return 0;
- }
1004 Ball(bitset优化,枚举)
思路:我们先吧每一条边都存起来,并且从小到大排个序(排序目的是避免后面的运算重复)然后对边进行遍历.当遍历的边是素数长度是进行一次check(这里判断是否为素数直接欧拉筛就行,数据范围1e5).
此时我们已经枚举了两个点并且确定了一条素数边了.那么如果要对答案做出贡献的话,剩余的点必须满足和之前选出的两个点的两条边一条大于素数边,一条小于素数边.用一个bitset维护来维护点集bitset<2048>visit[2048];第一维的状态表示一个点,第二维为01状态表示第一维的点和和第二维的下标组成的线段是否被遍历过了.
我们就将作为素数边的端点的两个点去找他们的相交的线段是否是一长一短,这个时候就可以考虑把遍历过的线段进行标记(用bitset置为1).那么下方代码中
visit[edg[i].po1]^visit[edg[i].po2]
的意思就是看选的第三个端点和两个素数边端点组成的端点的状况.如果是一长一短,因为是从小到大排列,而且遍历的也在bitset中置为1,所以只要满足这个式子结果为1,也就是一个遍历过一个没有遍历,就可以满足条件,得到1的数量就是可以组成的个数.
- #include
- #define int long long
- using namespace std;
- const int N =5e5+10,mod=998244353;
- bitset<2048>visit[2048];
- /*
- bitset存边,遍历过的即为1,未遍历即为0,只需要把边长度处理出来
- 之后排序(从小到大,这样就可以保证遍历过的边和没遍历过的在不同状态,bitset标记),
- 然后遍历,当该边为质数是用bitset进行异或(比他小的已经遍历了).结果加起来即可
- */
- struct point
- {
- int x,y;
- }poi[2048];
- struct edge
- {
- int po1,po2,len;
- }edg[5001000];
- int vis[200100],p[100100],cnt=0;
- void init()
- {
- vis[0]=vis[1]=1;
- for(int i=2;i<=200005;i++)
- {
- if(!vis[i])
- p[++cnt]=i;
- for(int j=1;j<=cnt&&i*p[j]<=200005;j++)
- {
- vis[i*p[j]]=1;
- if(i%p[j]==0)
- break;
- }
- }
- }
- void solve()
- {
- int n,m;
- cin>>n>>m;
-
- for(int i=1;i<=n;i++)
- cin>>poi[i].x>>poi[i].y,visit[i].reset();
- int tot=0;
- for(int i=1;i<=n;i++)
- {
- for(int j=i+1;j<=n;j++)
- {
- edg[++tot].po1=i;
- edg[tot].po2=j;
- edg[tot].len=abs(poi[i].x-poi[j].x)+abs(poi[i].y-poi[j].y);
- }
- }
- sort(edg+1,edg+tot+1,[&](edge a,edge b){
- return a.len
- });
- int ans=0;
- for(int i=1;i<=tot;i++)
- {
- if(vis[edg[i].len]==0)
- {
- ans+=(visit[edg[i].po1]^visit[edg[i].po2]).count();
- }
- visit[edg[i].po1].set(edg[i].po2);
- visit[edg[i].po2].set(edg[i].po1);
- }
- cout<
- return ;
- }
- signed main()
- {
- cin.tie(0);
- cout.tie(0);
- ios::sync_with_stdio(0);
- init();
- int t;
- cin>>t;
- while(t--)
- solve();
- return 0;
- }
1009 Laser(计算几何,枚举)
判断特殊情况+计算几何知识运用
- #include
- #define ll long long
- using namespace std;
- #define B cerr<<"Break Point"<
- #define pl(x) cerr<<#x<<' '<<"="<<' '<<(x)<
- #define p(x) cerr<<#x<<' '<<"="<<' '<<(x)<<' ';
- namespace io{
- const int SIZ=55;
- int que[SIZ],op,qr;
- char ch;
- template<class I>
- void read(I &w){
- ch=getchar(),op=1,w=0;
- while(!isdigit(ch)){if(ch=='-') op=-1;ch=getchar();}
- while(isdigit(ch)){w=w*10+ch-'0';ch=getchar();}w*=op;
- }
- template<typename T,typename... Args>
- void read(T& t,Args&... args){read(t);read(args...);}
- }
- using io::read;
- const int N=5e5+5;
- int T,n;
- struct node{
- int x,y;
- };
- node p[N];
- bool Check(int ox,int oy)
- {
- bool flag=true;
- for (int i = 1; i <= n; ++i)
- {
- if(p[i].x == ox) continue;
- if(p[i].y == oy) continue;
- if (abs(ox - p[i].x) == abs(oy - p[i].y)) continue;
- flag = false;
- }
- return flag;
- }
- int main()
- {
- read(T);
- while(T--)
- {
- read(n);
- for (int i = 1; i <= n; ++i)
- read(p[i].x, p[i].y);
- bool flag = true;
- int cur1, cur2;
- for (int i = 2; i <= n; ++i)
- {
- if(p[1].x == p[i].x) continue;
- if(p[1].y == p[i].y) continue;
- if(abs(p[1].x - p[i].x) == abs(p[1].y - p[i].y)) continue;//判断是否在写对角线上
- flag = false, cur1 = 1, cur2 = i;
- }
- if(flag)
- {
- puts("YES");
- continue;
- }
- int X1 = p[cur1].x, Y1 = p[cur1].y, X2 = p[cur2].x, Y2 = p[cur2].y;
- flag |= Check(X1, Y2);
- flag |= Check(X1, X1 - X2 + Y2);
- flag |= Check(X1, -X1 + X2 + Y2);
- flag |= Check(X2, Y1);
- flag |= Check(X2 + Y1 - Y2, Y1);
- flag |= Check(X2 - Y1 + Y2, Y1);
- flag |= Check(X2, -X1 + X2 + Y1);
- flag |= Check(X1 - Y1 + Y2, Y2);
- flag |= Check((X1 + X2 + Y2 - Y1) / 2, (-X1 + X2 + Y1 + Y2) / 2);
- flag |= Check(X2, X1 - X2 + Y1);
- flag |= Check(X1 + Y1 - Y2, Y2);
- flag |= Check((X1 + X2 + Y1 - Y2) / 2, (X1 - X2 + Y1 + Y2) / 2);
- //十二个相交硬判断
- if(flag) puts("YES");
- else puts("NO");
- }
- return 0;
- }
1011 Random(签到)
- #include
- #include
- using namespace std;
- #define ll long long
- const int MOD = 1e9 + 7;
- ll ksm(ll a,ll b)
- {
- ll ans = 1;
- while(b)
- {
- if(b & 1) ans = ans * a % MOD;
- a = a * a % MOD;
- b >>= 1;
- }
- return ans;
- }
- ll b;
- void solve()
- {
- ll n,m;
- cin >> n >> m;
- ll a = n - m;
- // if(a % 2 == 0)
- // {
- // printf("%lld\n",a / 2);
- // }
- // else
- // {
- printf("%lld\n",a * b % MOD);
- // }
- return ;
- }
- int main()
- {
- int T;
- b = ksm(2,MOD-2);
- scanf("%d",&T);
- while(T--) solve();
- return 0;
- }
1012 Alice and Bob
思路:可以把两个n看做一个(n-1),一次变化,最后看1的数量即可,如果1的个数大于1就是Alice硬,反之bob.
- #include
- #define int long long
- using namespace std;
- const int N =5e5+10,mod=998244353;
- int a[1001006];
- void solve()
- {
- int n;
- cin>>n;
- memset(a,0,n+10);
- for(int i=0;i<=n;i++)
- cin>>a[i];
- if(a[0]!=0)
- {
- cout<<"Alice\n";
- return ;
- }
- for(int i=n;i>1;i--)
- {
- int ans = a[i] / 2;
- a[i - 1] += ans;
- }
- if(a[1]> 1)
- {
- cout<<"Alice\n";
- return ;
- }
- cout<<"Bob\n";
- return ;
- }
- signed main()
- {
- int t;
- cin>>t;
- while(t--)
- solve();
- return 0;
- }
-
相关阅读:
rpm 系 linux 系统中 /etc/yum.repo.d/ 目录下的 .repo 文件中的 $releasever 到底等于多少?
MASA Stack 第五期社区例会
Java OpenJDK 8u392 Windows x64
vulnhub靶场之WORST WESTERN HOTEL: 1
基于JAVA全国消费水平展示平台计算机毕业设计源码+系统+mysql数据库+lw文档+部署
【考研数学】四. 多元函数微积分
Ant-Maven-Gradle
DERT:End-to-End Object Detection with Transformers
Vue项目中强制刷新页面的方法
分布式概念:编码一个简单分布式系统
-
原文地址:https://blog.csdn.net/qq_49593247/article/details/126055971