A:
问题描述:
给出两个人最多的分数分别为x,y且最小得分分别为a,b。求第一个获胜(分数高)所有的比分情况,按最小字典序输出比分。
解题思路:
暴力枚举,注意边界,可以先把比分存起来,然后再排序输出。
B:
问题描述:
给出n个区间,求这些区间中是否存在一个区间可以覆盖其他所有的区间,有输出是第几个区间;否则,输出-1
解题思路:
设L,R为前K-1个区间的最左和最右的边界,NUM为能覆盖其他的第X个区间,对于当前第K个,判断[KL,KR]是否能覆盖[L,R],能的话并记录是第几个区间NUM,
不能的话表示前K个没有一个能覆盖其他的,NUM=-1。更新[L,R] = [min(L,KL),max(R,KR)]。
最后,输出NUM。
C:
问题描述:
二维坐标下,给出起点和终点,和一些可以走的线段(总线段长度<=10^5)。求是否能到达,能的话时间最少是多少(第走一格花1个单位时间,可以向相邻的8个方向走)。
解题思路:
因为最多10^5个点,所以,map记录,暴力BFS。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<queue>
#include<set>
using namespace std;
#define clr(p,v) memset(p,v,sizeof(p))
#define MP make_pair
#define PII pair<int,int>
#define lson l, m, rt<<1
#define rson m+1,r,rt<<1|1
typedef __int64 LL ;
const int maxn = 100010 ;
const int maxm = 1000010 ;
const int inf = 1<<30 ;
int n,m,r,a,b;
int tot,cnt;
map<PII,int> step;
int dx[] = {-1,-1,-1,0,0,1,1,1};
int dy[] = {-1,0,1,-1,1,-1,0,1};
PII st,en;
queue<PII > q;
int main()
{
//freopen("k","r",stdin);
//input and init
step.clear();
scanf("%d%d%d%d",&st.first,&st.second,&en.first,&en.second);
scanf("%d",&n);
while(n--)
{
int r,x,y;
scanf("%d%d%d",&r,&x,&y);
for(int i=x;i<=y;++i)
step[MP(r,i)] = -1;
}
//solve BFS
while(!q.empty()) q.pop();
q.push(st);
step[st] = 0;
step[en] = -1;
while(!q.empty())
{
PII go,now;
now = q.front();
q.pop();
for(int i=0;i<8;++i)
{
go = MP(now.first+dx[i], now.second+dy[i]);
if(step.count(go) && step[go]==-1)
{
step[go] = step[now]+1;
q.push(go);
}
}
}
printf("%d\n",step[en]);
return 0;
}
D:
问题描述:
给出一个按纽的无向图,N个点,M条边。起初每个按纽显示的值是0,每按一下一个按纽,该按纽及跟它直接相邻的按纽显示的数值+1,且每个按纽最多只能按一次。
现在给出一个N个点序列{A1,A2,。。。AN} ,要求最后按纽的显示不会出现 : 第K个的值==AK (1<=K<=N)。
存在的话,输出一个符合条件的按按纽方案;否则,输出-1。
解题思路:
因为序列中每一个都只有一个值,而一个按纽可以选择按或不按两种情况,至少有两个值,所以答案一定存在。
我的方法是:先把序列为0的先放入队列,然后按规则加,再把不符合的加入队列,重复这个过程,直到符合条件为止。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<queue>
using namespace std;
#define clr(p,v) memset(p,v,sizeof(p))
#define LL(rt) rt<<1
#define RR(rt) rt<<1|1
typedef __int64 LL ;
const int maxn = 100010 ;
const int maxm = 1000010 ;
const int inf = 1<<30 ;
int n,m;
int tot,cnt;
int first[maxn],d[maxn],ans[maxn],num[maxn];
int to[maxm<<2],next[maxm<<2];
bool vis[maxn];
void init()
{
tot = 0;
clr(first,-1);
}
void add(int x,int y)
{
to[tot]=y;next[tot]=first[x];first[x]=tot++;
to[tot]=x;next[tot]=first[y];first[y]=tot++;
}
int main()
{
scanf("%d%d",&n,&m);
init();
clr(num,0);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
queue<int> q;
cnt = 0;
clr(vis,true);
for(int i=1;i<=n;i++)
{
scanf("%d",&d[i]);
if(d[i] == 0)
{
vis[i] = false;
q.push(i);
}
}
//
while(!q.empty())
{
int cur = q.front();
q.pop();
if(num[cur] != d[cur]) continue;
ans[cnt++] = cur;
for(int i=first[cur];i!=-1;i=next[i])
{
int vv = to[i];
++num[vv];
if(vis[vv] && num[vv]==d[vv])
{
vis[vv] = false;
q.push(vv);
}
}
}
printf("%d\n",cnt);
for(int i=0;i<cnt;i++)
{
if(i) printf(" %d",ans[i]);
else printf("%d",ans[i]);
}
puts("");
return 0;
}
E:
问题描述:
给出一个含有N个数的序列,M个操作,操作有两种形式:
1 L R 询问数列[L,R]之间的和,并输出这个和;
2 L R X 数列[L,R]间所有数异或X A[L]^X,A[L+1]^X..........A[R]^X。
解题思路:
因为最大值<=10^6,所以,拆位建20棵线段树。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
#include<string>
#include<queue>
#include<set>
using namespace std;
#define clr(p,v) memset(p,v,sizeof(p))
#define MP make_pair
#define PII pair<int,int>
#define lson l, m, rt<<1
#define rson m+1,r,rt<<1|1
typedef __int64 LL ;
const int maxn = 100010 ;
const int maxm = 1000010 ;
const int inf = 1<<30 ;
int n,m,r,a,b;
int tot,cnt;
int col[maxn<<2],sum[20][maxn<<2];
void PushDown(int rt,int tl,int tr)
{
if(col[rt])
{
int ll = rt<<1;
int rr = ll|1;
//
int tt,cc;
for(int i=0;i<20;++i)
{
cc = 1<<i;
if(cc > col[rt]) break;
tt = cc&col[rt];
if(tt)
{
sum[i][ll] = tl-sum[i][ll];
sum[i][rr] = tr-sum[i][rr];
}
}
col[ll] ^= col[rt];
col[rr] ^= col[rt];
col[rt] = 0;
}
}
void PushUp(int rt)
{
int ll = rt<<1;
int rr = ll|1;
for(int i=0;i<20;++i)
sum[i][rt] = sum[i][ll]+sum[i][rr];
}
void build(int l,int r,int rt)
{
col[rt] = 0;
for(int i=0;i<20;++i) sum[i][rt] = 0;
if(l == r)
{
int x,cc;
scanf("%d",&x);
for(int i=0;i<20;i++)
{
cc = 1<<i;
if(cc > x) break;
int tt = cc&x;
if(tt) ++sum[i][rt];
}
return;
}
//
int m = (l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int x,int l,int r,int rt)
{
if(L<=l && r<=R)
{
int tt,cc;
int tot = r-l+1;
for(int i=0;i<20;++i)
{
tt = 0;
cc = 1<<i;
if(cc > x) break;
tt = cc&x;
if(tt)
sum[i][rt] = tot-sum[i][rt];
}
col[rt] ^= x;
return;
}
//
int m = (l+r)>>1;
PushDown(rt,m-l+1,r-m);
if(L<=m) update(L,R,x,lson);
if(m< R) update(L,R,x,rson);
PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
LL total = 0;
if(L<=l && r<=R)
{
LL tt;
for(int i=0;i<20;++i)
{
tt = (LL)(1<<i);
total += tt*sum[i][rt];
}
return total;
}
//
int m = (l+r)>>1;
PushDown(rt,m-l+1,r-m);
if(L<=m)
total += query(L,R,lson);
if(m< R)
total += query(L,R,rson);
return total;
}
int main()
{
//freopen("k","r",stdin);
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
int x,y,z;
scanf("%d",&z);
if(z == 1)
{
scanf("%d%d",&x,&y);
printf("%I64d\n",query(x,y,1,n,1));
}
else
{
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,1,n,1);
}
}
return 0;
}