爆炸的一场,脑子已经放弃我了。。
#include
using namespace std;
const int maxn = 1e6+10;
int to[maxn], ans[maxn];
bool vis[maxn], ins[maxn];//ins标记是否在当前处理的栈中
stack<int> s;
int n;
int dfs(int x)
{
if(ans[x]) return ans[x];
else return ans[x] = dfs(to[x])+1;
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &to[i]);
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
int j = i;
while(!vis[j])//终止的条件:在本次处理中出现环/当前的j在之前已经被处理过
{
s.push(j);
ins[j] = vis[j] = true;
j = to[j];
}
if(ins[j])//本次处理中出现环,j->..->j
{
int cnt = 1, oj = j;
while(to[j]!=oj) cnt++, j = to[j];
ans[oj] = cnt; j = oj;
while(to[j]!=oj) j = to[j], ans[j] = cnt;//直接统计环内答案
}
while(!s.empty()) ins[s.top()] = false, s.pop();
}
}
int res = 0;
for(int i = 1; i <= n; i++)//从每个点出发的最长简单路径,记忆化
res = max(res, dfs(i));
cout << res;
return 0;
}
I | Y[] |
---|---|
1 | {1} |
2 | {2} |
3 | {5} |
4 | {5, 4} |
5 | {5, 4, 3} |
6 | {9, 4, 3} |
7 | {9, 7, 3} |
8 | {9, 7, 6} |
所以最终分组为{1, 2, 5, 9} 、{4, 7}、{3, 6} 组数为3
#include
using namespace std;
const int maxn = 1e5+10;
struct node{
int x, y, oid;
friend bool operator < (node a, node b)
{
return a.x < b.x;
}
}a[maxn];
int Y[maxn], ans[maxn];
int n;
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d %d", &a[i].x, &a[i].y), a[i].oid = i;
sort(a+1, a+1+n);
int len = 0;
Y[0] = INT_MAX;
for(int i = 1; i <= n; i++)
{
//cout << a[i].y << " " << Y[len] << endl;
if(a[i].y<Y[len]) Y[++len] = a[i].y, ans[a[i].oid] = len;
else
{
int it = lower_bound(Y+1, Y+1+len, a[i].y, greater<int>()) - Y;//第一个小于x的下标
Y[it] = a[i].y;
ans[a[i].oid] = it;
}
}
printf("%d\n", len);
for(int i = 1; i <= n; i++)
printf("%d%c", ans[i], i==n?'\n':' ');
return 0;
}
#include
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
int v[maxn], prime[maxn];
ll sc[maxn];
int m = 0;
inline ll read()
{
ll s = 0, f = 1;
char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = - 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) s = ((s << 3ll) + (s << 1ll) + (ch ^ 48ll));
return s * f ;
}
void pre()//5035个质数
{
for(int i = 2; i <= 49000; i++)
{
if(v[i] == 0)
{
v[i] = i;
prime[++m] = i;
sc[m] = 1ll*i*i*i;
}
for(int j = 1; j <= m; j++)
{
if(prime[j]>v[i] || prime[j]*i>1e6) break;
v[i*prime[j]] = prime[j];
}
}
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
pre();
int t; ll n;
scanf("%d", &t);
while(t--)
{
//scanf("%lld", &n);
n = read();
ll ans = 1;
for(int i = 1; i <= m && n>=prime[i]; i++)
{
if(n%prime[i]==0)
{
while(n%sc[i]==0)
{
ans *= prime[i];
n /= sc[i];
}
while(n%prime[i]==0) n/=prime[i];
}
}
ll l = 1, r = 1e6;
while(l<=r)
{
ll mid = (l+r)>>1;
if(mid*mid*mid<n) l = mid+1;
else r = mid-1;
}
if(l*l*l==n) ans *= l;
printf("%lld\n", ans);
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int maxn = 4e5+10;
struct node{
int x, lr, type;//lr=1 投影右端
friend bool operator < (node a, node b)
{
if(a.x==b.x) return a.lr > b.lr;
return a.x < b.x;
}
}a[maxn];
ll cnt[3];
inline int read()
{
int s = 0, f = 1;
char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = - 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) s = ((s << 3ll) + (s << 1ll) + (ch ^ 48ll));
return s * f ;
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
int n, m, x, y, p, q;
scanf("%d %d", &n, &m);
int num = 0;
for(int i = 1; i <= n; i++)
{
x = read(), y = read(), p = read(), q = read();
a[++num] = node{y-x, -1, 0};
a[++num] = node{q-p, 1, 0};
}
for(int i = 1; i <= m; i++)
{
x = read(), y = read(), p = read(), q = read();
a[++num] = node{y-x, -1, 1};
a[++num] = node{q-p, 1, 1};
}
sort(a+1, a+1+num);
ll ans = 0;
for(int i = 1; i <= num; i++)
{
cnt[a[i].type] += a[i].lr;
if(a[i].lr==1) ans += cnt[a[i].type^1];
}
cout << ans;
return 0;
}
#include
using namespace std;
const int maxn = 510;
struct node{
int x, y, val;
friend bool operator < (node a, node b)
{
return a.val < b.val;
}
}edge[maxn*(maxn-1)/2+10];
struct NODE{
int id, val;
};
vector<NODE> link[maxn];//与当前节点相连的所有点及其距离
int fa[maxn], ans[maxn], a[maxn][maxn], dis[maxn][maxn];
int n;
int find_fa(int x)
{
return fa[x]==x ? x : fa[x]=find_fa(fa[x]);
}
void dfs(int st, int now, int pre, int d)
{
dis[st][now] = d;
for(int i = 0; i < link[now].size(); i++)
{
int nxt = link[now][i].id, val = link[now][i].val;
if(nxt!=pre) dfs(st, nxt, now, d+val);
}
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
scanf("%d", &n);
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
int num = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[i][j]!=a[j][i])//题目没有保证输入的矩阵绝对是对称的
{
printf("No\n");
return 0;
}
if(i<j) edge[++num] = node{i, j, a[i][j]};
}
}
sort(edge+1, edge+1+num);
for(int i = 1, cnt = 0; i <= num; i++)
{
int x = find_fa(edge[i].x), y = find_fa(edge[i].y);
if(x!=y)
{
fa[x] = y;
int xx = edge[i].x, yy = edge[i].y, val = edge[i].val;
ans[++cnt] = val;
link[xx].push_back(NODE{yy, val});
link[yy].push_back(NODE{xx, val});
}
}
for(int i = 1; i <= n; i++) dfs(i, i, 0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(a[i][j]!=dis[i][j])
{
printf("No\n");
return 0;
}
printf("Yes\n");
for(int i = 1; i < n; i++) printf("%d\n", ans[i]);
return 0;
}