T2
按照题意模拟即可,不过因为是个环,从0开始会方便一些
ACcode:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int cnt, pos, sum, num;
int ans=INT_MIN;
const int N=105;
int n, a[N];
bool vis[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
{
pos = i;
cnt = sum = num = 0;
memset(vis, 0, sizeof vis);
while (cnt < n && num < n)
{
cnt++;
while (vis[pos])
pos = (pos + 1) % n;
if (cnt == a[pos])
{
vis[pos] = 1;
cnt = 0;
sum += a[pos];
num++;
}
pos = (pos + 1) % n;
}
ans = max(ans, sum);
}
cout << ans;
return 0;
}T3
拓扑板子题
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 505;
vector <int> v[N];
queue <int> q;
int num[N];
bool G[N];
int n, cnt;
void topo()
{
for (int i = 0; i <= 500; i++)
{
if (num[i] == 0)
q.push(i);
}
while (!q.empty())
{
int hh = q.front();
q.pop();
if (G[hh] == 1) cnt--;
int len = v[hh].size();
for (int i = 0; i < len; ++i)
{
num[v[hh][i]]--;
if (num[v[hh][i]] == 0)
q.push(v[hh][i]);
}
}
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cnt = n;
for (int i = 1; i <= n; i++)
{
int u, m;
cin >> u >> m;
G[u] = 1;
for (int j = 1; j <= m; j++)
{
int y;
cin >> y;
v[u].push_back(y);
num[y]++;
}
}
topo();
if (cnt <= 0)
cout << "YES";
else
cout << cnt;
return 0;
}T4
还是模拟,因为题目已经把牛和fj的走路特点告诉你了,然后一直while (1)就可以了
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=15;
char a[N][N];
int nx[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
signed main()
{
for(int i=1;i<=10;i++)
cin>>a[i]+1;
for(int i=0;i<=11;i++)
for(int j=0;j<=11;j++)
if(i==0||j==0||i==11||j==11)
a[i][j]='*';
int fx=0,fy=0,cx=0,cy=0,fd=0,cd=0;
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
{
if(a[i][j]=='F')
{
fx=i;
fy=j;
}
else if(a[i][j]=='C')
{
cx=i;
cy=j;
}
}
int cnt=0,flag=0;
while(++cnt)
{
int nfx=fx+nx[fd][0],nfy=nx[fd][1]+fy;
if(a[nfx][nfy]=='*') //花一秒来转弯
fd=(fd+1)%4;
else //直接向前走
{
fx=nfx;
fy=nfy;
}
int cnx=cx+nx[cd][0],ncy=cy+nx[cd][1];
if(a[cnx][ncy]=='*')
cd=(cd+1)%4;
else
{
cx=cnx;
cy=ncy;
}
if(cx==fx&&cy==fy)
{
flag=1;
break;
}
if(cnt==1000) //运行了足够多次还没结果,直接看为无解
break;
}
if(flag==1)
cout<<cnt;
else
cout<<0;
return 0;
}T5
很明显,直接双指针是不行的
考试的时候后面那个人还说这题好水
因为数组开不下,肯定用不了前缀和,所以可以用等差数列求和公式:(l + r) * (r - l + 1) / 2
然后带入,因为不停打公式太烦了,可以用一个函数包起来
#include<bits/stdc++.h>
#define int long long
using namespace std;
int cal(int r,int l){return (r+r+l-1)*l/2;} //从r开始的区间长度为l的区间和
signed main()
{
int m,n;
cin>>n>>m;
for(int len=1000000;len>=1;len--)
{
int l=0,r=n+1; //二分
while(l+1<r)
{
int mid=l+r>>1;
if(cal(mid,len)>=m)
r=mid;
else
l=mid;
}
if(cal(r,len)==m && r+len-1<=n)
printf("[%lld,%lld]\n",r,r+len-1);
}
return 0;
}