就是双指针,左指针从 0 开始,右指针从 开始,然后 循环,运行条件是左指针小于右指针。每次答案都加上 (a[i] - a[j]) * (a[i] - a[j]) (i 为左指针,j 为右指针),然后左指针减一,右指针加一。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 305;
int a[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort (a + 1 , a + n + 1);
int maxn = n - 1 , minn = 2;
int now = n , last = 0;
int cnt = 0 , ans = 0;
while (cnt < n)
{
// cout << now << " ";
cnt++;
ans += (a[now] - last) * (a[now] - last);
last = now;
if (cnt % 2 == 1)
{
now = minn;
minn++;
}
else
{
now = maxn;
maxn--;
}
// cout << ans << " " << maxn << " " << minn << endl;
}
// cout << endl;
cout << ans;
return 0;
}双指针错误。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 305;
int a[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort (a + 1 , a + n + 1);
int i = 0 , j = n;
int ans = 0;
while (i < j)
{
ans += (a[i] - a[j]) * (a[i] - a[j]);
i++;
ans += (a[i] - a[j]) * (a[i] - a[j]);
j--;
}
cout << ans;
return 0;
}典型 题~~
这道题数据范围比较大,需要加记忆化和预处理(不加的话会 )
先加一个记忆化,然后枚举上下左右四个方向,并且判断当前位置满不满足要求,如果满足要求,就 。
这里要加一个预处理,因为如果不加预处理,在要输出答案的时候,就会超时。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
char a[N][N];
bool vis[N][N];
int n , m;
int f[N][N];
int dx[] = {0 , 0 , 1 , -1};
int dy[] = {1 , -1 , 0 , 0};
int cnt = 0;
void dfs(int x , int y)
{
if (vis[x][y] == 1)
{
return ;
}
vis[x][y] = 1;
cnt++;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx <= 0 || ny <= 0 || nx > n || ny > n || a[nx][ny] == a[x][y] || vis[nx][ny] == 1)
{
continue;
}
dfs(nx , ny);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
while (m--)
{
int x , y;
cin >> x >> y;
memset(vis , 0 , sizeof vis);
dfs(x , y);
cout << cnt << endl;
cnt = 0;
}
return 0;
}没加预处理和记忆化导致超时。
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n , m;
char a[N][N];
int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};
int vis[N][N] , s[N * N];
int cnt = 0;
void dfs(int x , int y , char z)
{
vis[x][y] = cnt;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (vis[nx][ny] != 0)
{
continue;
}
if (nx < 1 || ny < 1 || nx > n || ny > n)
{
continue;
}
if (a[nx][ny] == z)
{
continue;
}
dfs(nx , ny , a[nx][ny]);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (vis[i][j] == 0)
{
cnt++;
dfs(i , j , a[i][j]);
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
s[vis[i][j]]++;
}
}
while (m--)
{
int x , y;
cin >> x >> y;
cout << s[vis[x][y]] << endl;
}
return 0;
}这是一道并查集的题。
我们主要是来判断有没有环(如果有情况是 要站在 前面并且 也要站在 前面就可以说是有环)。
然后就找联通块,最后输出联通块个数。
当然,如果有环,就直接输出 0,不用找联通块。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5 , MOD = 1e9 + 7;
int fa[N] , l[N] , r[N];
set <pair<int , int> >st;
int get(int x)
{
if (x == fa[x])
{
return x;
}
return fa[x] = get(fa[x]);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
while (m--)
{
int u , v;
cin >> u >> v;
if (st.count({u , v}))
{
continue;
}
st.insert({u , v});
int t1 = get(u) , t2 = get(v);
if (r[u] != 0 || l[v] != 0 || t1 == t2)
{
cout << 0 << endl;
return 0;
}
r[u] = v;
l[v] = u;
fa[t1] = t2;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (fa[i] == i)
{
cnt++;
}
}
int ans = 1;
for (int i = 1; i <= cnt; i++)
{
ans = ans * i % MOD;
}
cout << ans;
return 0;
}这就一纯纯大模拟!!!
先造一个临时数组 ,然后让 tmp[i][j] = b[n - j + 1][j](b数组为原来的数组)。
就两次旋转90°。
就三次旋转90°。
tmp数组为临时数组,那么tmp[j][i] = b[n - i + 1][j]。
先翻转,再尝试 1 ~ 3。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
char a[N][N] , ans[N][N] , b[N][N];
int n;
bool check(char b[][N])
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (b[i][j] != ans[i][j])
{
return 0;
}
}
}
return 1;
}
void xuan(char b[][N] , char c[][N])
{
char tmp[N][N];
for (int j = 1; j <= n; j++)
{
for (int i = 1; i <= n; i++)
{
tmp[i][j] = b[n - j + 1][i];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
c[i][j] = tmp[i][j];
}
}
}
void fan(char b[][N] , char c[][N])
{
char tmp[N][N];
for (int j = 1; j <= n; j++)
{
for (int i = 1; i <= n; i++)
{
tmp[i][j] = b[i][n - j + 1];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
c[i][j] = tmp[i][j];
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> ans[i][j];
}
}
xuan(a , b);
if (check(b) == 1)
{
cout << 1;
return 0;
}
xuan(b , b);
if (check(b) == 1)
{
cout << 2;
return 0;
}
xuan(b , b);
if (check(b) == 1)
{
cout << 3;
return 0;
}
fan(a , b);
if (check(b) == 1)
{
cout << 4;
return 0;
}
xuan(b , b);
if (check(b) == 1)
{
cout << 5;
return 0;
}
xuan(b , b);
if (check(b) == 1)
{
cout << 5;
return 0;
}
xuan(b , b);
if (check(b) == 1)
{
cout << 5;
return 0;
}
if (check(a) == 1)
{
cout << 6;
return 0;
}
cout << 7;
return 0;
}