Summer training round2 #9(Training28)
A:签到题
C:模拟搜索题
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=;
int year[];
int where[];
int visit[];
vector<int> manager[];
int anser=;
void dfs(int x)
{
visit[x]=;
int len=manager[x].size();
for(int i=;i<len;i++)
{
int to=manager[x][i];
if(!visit[to])
{
anser=min(anser,year[to]);
dfs(to);
}
}
}
int main()
{
for(int i=;i<=;i++)
where[i]=i;
int n,m,I;
int from,to;
cin >> n >>m >>I;
for(int i=;i<=n;i++)
cin >>year[i];
for(int i=;i<=m;i++)
{
scanf("%d %d",&from,&to);
manager[to].pb(from);
}
string a;
int cnt;
for(int i=;i<=I;i++)
{
cin >> a;
if(a[]=='T')
{
cin >> from >>to;
swap(where[from],where[to]);
swap(year[where[from]],year[where[to]]);
}
else if(a[]=='P')
{
mem(visit,);
cin >> from;
anser=;
dfs(where[from]);
if(anser==)
cout<<"*"<<endl;
else
cout<<anser<<endl;
}
}
}
D:队友
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define INF 2139062143
#define inf -2139062144
#define ll long long
#include<vector>
using namespace std;
map<vector<int> , int> ma;
vector<int> rr;
bool jj = false;
vector<int> fold(int p,vector<int> qq) {
int l = qq.size();
vector<int> re;
for(int i=; i<=l; i++) {
int tt = ;
bool flag = false;
if(p - i >= ) {
tt+=qq[p-i];
flag = true;
}
if(p + i - < l) {
tt+=qq[p + i - ];
flag = true;
}
if(flag) re.insert(re.begin(),tt);
}
return re;
}
void dfs(vector<int> qq) {
int llen = qq.size();
int i,j;
for(i=; i<=llen && jj == false; i++) {
vector<int> tem = fold(i,qq);
if(tem == rr) {
jj = true;
break;
}
if(ma[tem] == ) {
ma[tem] = ;
dfs(tem);
}
}
}
int main() {
vector<int> q;
int n,i,j;
scanf("%d",&n);
for(i=; i<n; i++) {
int a;
scanf("%d",&a);
q.push_back(a);
}
int m;
scanf("%d",&m);
for(i=; i<m; i++) {
int a;
scanf("%d",&a);
rr.push_back(a);
}
dfs(q);
// for(i=0; i<=n; i++) {
// vector<int> tem = fold(i);
// for(j=0; j<tem.size(); j++) {
// printf("%d ",tem[j]);
// }
// printf("\n");
// if(tem == rr) {
// jj = true;
// break;
// }
// }
if(jj) printf("S\n");
else printf("N\n");
return ;
}
E:水题
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=;
int a[];
int visit[];
queue<int> que;
int main()
{
mem(visit,);
int r,n;
cin >>r >> n;
int now;
for(int i=;i<=n;i++)
{
scanf("%d",&now);
visit[now]=;
}
for(int i=;i<=r;i++)
if(!visit[i])
que.push(i);
int len=que.size();
if(len==)
{
cout<<"*"<<endl;
return ;
}
while(!que.empty())
{
cout<<que.front()<<" ";
que.pop();
}
cout<<endl;
}
F:队友
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define INF 2139062143
#define inf -2139062144
#define ll long long
using namespace std;
int a[];
int p[];
map<int,int> q;
int main(){
int n,i,j;
scanf("%d",&n);
int sum = ;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
sum += a[i];
p[i] = sum;
q[sum] = ;
// printf("%d ",sum);
}
// printf("\n");
a[] = ;
q[] = ;
p[] = ;
if(sum % != ){
printf("0\n");
return ;
}
int len = sum / ;
int ans = ;
for(i = ;i < n;i++){
// printf("%d %d %d\n",p[i],(p[i] + len) % sum,(p[i] + len * 2) % sum);
if(q[(p[i] + len) % sum] == && q[(p[i] + len * ) % sum] == ){
ans++;
}
}
printf("%d\n",ans/);
return ;
}
G:队友
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val, id;
bool operator < (const Node & rhs) const {
return val < rhs.val;
}
}a[];
int L, C, X[][], cnt_l[], cnt_c[], l[], c[], b[];
int Solve(int * array, int n) {
for(int i = ; i <= n; i++) a[i].val = array[i], a[i].id = i;
sort(a + , a + + n);
for(int i = ; i <= n; i++) b[i] = a[i].id;
int ans = ;
for(int i = ; i <= n ;i++) {
if(b[i] != i) {
for(int j = i; j <= n; j++) {
if(b[j] == i) {
swap(b[j], b[i]);
ans++;
break;
}
}
}
}
return ans;
}
int main() {
scanf("%d%d", &L, &C);
for(int i = ; i <= L; i++) {
for(int j = ; j <= C; j++) {
scanf("%d", &X[i][j]);
}
}
int flag1 = , flag2 = , cnt, sum;
for(int i = ; i <= L; i++) {
cnt_l[(i - ) * C * C + ( + C) * C / ] = ;
//cout << (i - 1) * C * C + (1 + C) * C / 2 << endl;
}
for(int i = ; i <= C; i++) {
cnt_c[i * L + C * (L - ) * L / ] = ;
//cout << i * L + C * (L - 1) * L / 2 << endl;
}
cnt = ;
for(int i = ; i <= L; i++) {
sum = ;
for(int j = ; j <= C; j++) {
sum += X[i][j];
}
//cout << sum << endl;
if(cnt_l[sum]) cnt++;
}
//cout << cnt << endl;
if(cnt == L) flag1 = ;
cnt = ;
for(int j = ; j <= C; j++) {
sum = ;
for(int i = ; i <= L; i++) {
sum += X[i][j];
}
if(cnt_c[sum]) cnt++;
}
if(cnt == C) flag2 = ;
//cout << flag1 << " " << flag2 << endl;
if(flag1 && flag2) {
for(int i = ; i <= L; i++) {
l[i] = X[i][];
}
for(int i = ; i <= C; i++) {
c[i] = X[][i];
}
int ans = ;
ans += Solve(l, L);
ans += Solve(c, C);
printf("%d\n", ans);
}else {
printf("*\n");
}
return ;
}
H:数论题 矩阵快速幂
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 2139062143
#define inf -2139062144
#define ll long long
using namespace std;
ll mod = ;
struct matrix {
ll sizex,sizey;
ll num[][];
};
matrix mul(matrix a,matrix b) {
matrix result;
result.sizex = a.sizex;
result.sizey = b.sizey;
ll i,j,k;
for(i=; i<a.sizex; i++) {
for(j=; j<b.sizey; j++) {
ll sum = ;
for(k=; k<a.sizey; k++) sum+=a.num[i][k]*b.num[k][j] % mod;
result.num[i][j] = sum % mod;
}
}
return result;
} matrix zero(ll size) {
ll i,j;
matrix result;
result.sizex = size;
result.sizey = size;
for(i=; i<size; i++)
for(j=; j<size; j++)
result.num[i][j] = ;
return result;
} matrix eye(ll size) {
matrix result;
result.sizex = size;
result.sizey = size;
result = zero(size);
for(ll i=; i<size; i++) {
result.num[i][i] = ;
}
return result;
} matrix quickmat(matrix a,ll k) {
matrix origin = a;
matrix res;
res = eye(a.sizex);
while(k) {
if (k & ) {
res=mul(res,origin);
}
k>>=;
origin=mul(origin,origin);
}
return res;
} matrix initial(ll *a,ll sizex,ll sizey) {
ll i,j;
matrix result;
result.sizex = sizex;
result.sizey = sizey;
ll count = ;
for(i=; i<sizex; i++)
for(j=; j<sizey; j++)
result.num[i][j] = a[count++];
return result;
} ll all_ele_sum(matrix a) {
ll re = ;
for(ll i = ; i<a.sizex; i++) {
for(ll j = ; j<a.sizey; j++) {
re+=a.num[i][j];
}
}
return re;
} void printmatrix(matrix a) {
for(ll i = ; i<a.sizex; i++) {
for(ll j = ; j<a.sizey; j++) {
printf("%lld ",a.num[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
ll n,k,l;
scanf("%lld%lld%lld",&n,&k,&l);
n /= ;
k %= mod;
l %= mod;
ll a[] = {k,l,,};
if(n == ){
printf("%06lld\n",a[]);
return ;
}
ll x2 = (k * k % mod + l) % mod;
if(n == ){
printf("%06lld\n",x2);
return ;
}
matrix temp = initial(a,,);
matrix re = quickmat(temp,n-);
ll ans = (re.num[][] * x2 % mod + re.num[][] * k % mod) % mod;
printf("%06lld\n",ans);
return ;
}
I:类搜 DP
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=;
int f[maxn];
int n;
int c;
int t1,t2;
int tmax,tmin;
ll ans[maxn];
ll anser;
ll get(int x)
{
if(x>n)
return ;
if(ans[x]!=-)
return ans[x];
ans[x]=<<;
for(int i=x;i<=n;i++)
{
ll cha=f[i]-f[x];
if(cha>tmax)
break;
if(cha<=t1)
ans[x]=min(ans[x],get(i+)+t1);
if(cha<=t2)
ans[x]=min(ans[x],get(i+)+t2);
}
return ans[x];
}
int main()
{
mem(ans,-);
cin >> n >> c >> t1 >> t2;
tmax=max(t1,t2);
tmin=min(t1,t2);
for(int i=;i<=n;i++)
cin >> f[i];
sort(f+,f++n);
ll anser=get();
cout<<anser<<endl;
}
J:LCA/LCT
#include <bits/stdc++.h>
#define MAXN 20005
#define MAXM 100005
#define inf 0x3f3f3f3f
using namespace std; int n, m, s;
int head[MAXN], cnt;
int fa[MAXN];
int ancient[MAXN][], minw[MAXN][], dep[MAXN]; struct Node {
int u, v, w;
bool operator<(const Node &rhs) const {
return w > rhs.w;
}
} ori_edge[MAXM]; struct Edge {
int to, nxt, w;
} edge[MAXM * ]; int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
} void AddEdge(int u, int v, int w) {
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].nxt = head[u];
head[u] = cnt++;
} void Add(int u, int v, int w) {
AddEdge(u, v, w);
AddEdge(v, u, w);
} void Init() {
memset(head, -, sizeof(head));
cnt = ;
for (int i = ; i <= n; i++) fa[i] = i;
} void Kruskal() {
sort(ori_edge + , ori_edge + m + );
int cnt = ;
for (int i = ; i <= m; i++) {
int f1 = Find(ori_edge[i].u);
int f2 = Find(ori_edge[i].v);
if (f1 != f2) {
fa[f1] = f2;
Add(ori_edge[i].u, ori_edge[i].v, ori_edge[i].w);
cnt++;
if (cnt == n - ) break;
}
}
} void Dfs(int u, int f, int d) {
fa[u] = f;
dep[u] = d;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (v == f) continue;
minw[v][] = edge[i].w;
Dfs(v, u, d + );
}
} void Pre() {
memset(ancient, -, sizeof(ancient));
for (int i = ; i <= n; i++) {
ancient[i][] = fa[i];
}
minw[][] = inf;
for (int j = ; ( << j) <= n; j++) {
for (int i = ; i <= n; i++) {
if (~ancient[i][j - ]) {
int f = ancient[i][j - ];
ancient[i][j] = ancient[f][j - ];
minw[i][j] = min(minw[i][j - ], minw[f][j - ]);
}
}
}
} int Query(int p, int q) {
if (dep[p] < dep[q]) swap(p, q);
int k = ;
while (( << k) < dep[p]) k++;
int ans = inf;
for (int i = k; i >= ; i--) {
if (~ancient[p][i] && dep[ancient[p][i]] >= dep[q]) {
ans = min(ans, minw[p][i]), p = ancient[p][i];
} } if (p == q) return ans;
for (int i = k; i >= ; i--) {
if (~ancient[p][i] && ancient[p][i] != ancient[q][i]) {
ans = min(ans, minw[p][i]);
p = ancient[p][i];
ans = min(ans, minw[q][i]);
q = ancient[q][i];
}
} if (p != q) {
ans = min(ans, minw[p][]);
ans = min(ans, minw[q][]);
}
return ans;
} int main() {
scanf("%d%d%d", &n, &m, &s);
Init();
for (int i = ; i <= m; i++) {
scanf("%d%d%d", &ori_edge[i].u, &ori_edge[i].v, &ori_edge[i].w);
}
Kruskal();
Dfs(, -, );
Pre();
for (int i = ; i <= s; i++) {
int l, h;
scanf("%d%d", &l, &h);
printf("%d\n", Query(l, h));
}
return ;
}
Summer training round2 #9(Training28)的更多相关文章
- Summer training round2 #3
A!: GTY系列题 B!:莫队加分块 GTY系列题 C!:线段树模拟拓扑排序(把普通的拓扑排序的栈操作改成线段树区间减一,查询区间最右侧的0的位置即可.注意一 ...
- Summer training round2 #1
A:水 B:求两个三角形之间的位置关系:相交 相离 内含 ①用三个点是否在三角形内外判断 计算MA*MB.MB*MC.MC*MA的大小 若这三个值同号,那么在三角形的内部,异号在外部 #incl ...
- Summer training round2 #8(Training26)
A:贪心DFS 先从最远的搜起 如果一个点的value>=2 就ans++ D:并查集 E:大模拟 F:快速幂 #include <bits/stdc++.h> using name ...
- Summer training round2 #10(Training 30)
A:签到题 B!:搜索+DP #include<bits/stdc++.h> #define mp make_pair #define pi pair<int,int> usi ...
- Summer training round2 #7 (Training #23)
A:约瑟夫环 套公式 B:线性筛素数 C:投骰子 概率DP F:有权无向图的生成树(边最大值和最小值只差最小) 直接kruskal G:状压BFS或者双向BFS H:模拟题 I:几何题 J:高斯消元
- Summer training round2 #6 (Training #22)
A:二分答案 如果中位数比目前的大就right=mid-1 else left=mid+1 C!:几何 G:优先队列贪心 #include <bits/stdc++.h> using na ...
- Summer training round2 #5 (Training #21)
A:正着DFS一次处理出每个节点有多少个优先级比他低的(包括自己)作为值v[i] 求A B 再反着DFS求优先级比自己高的求C #include <bits/stdc++.h> #incl ...
- Summer training round2 #4 (Training #20)
A!:UESTC1752 B!:找区间内L到R之间内的数的个数 权值分块加莫队 C!:给你一个哈斯图 去掉其中的几条边 要求输出字典序最大的拓扑排序:线段树模拟拓扑排序 D!:要求你找到最短路树并输 ...
- hdu 4946 2014 Multi-University Training Contest 8
Area of Mushroom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Head First HTML 与 CSS 学习笔记
总的来说,这本书作为入门HTML和CSS还是不错的,讲解生动有趣,这也是Head First系列书籍的特点.缺点就是讲解的不够全面~ 第三章 一般来讲,如果要引用一段或者多段文字, 就要使用<b ...
- 转:async异步、thread多线程
很全面的知识,转来留着 1:https://www.cnblogs.com/xibei/p/11826498.html 2:https://www.cnblogs.com/xibei/p/118742 ...
- 直方图匹配原理与python、matlab实现
直方图匹配本质上是让两幅图像的累积直方图尽量相似,累积直方图相似了,直方图也就相似了. 把原图像img的直方图匹配到参考图像ref的直方图,包括以下几个步骤: 1. 求出原图像img的累积直方图img ...
- python基础之编码
ascci:字母.数字.特色字符,1个字节-8位Unicode:两个字节-16位,升级版四个字节-32位uft-8:最少一个字节-8位,英文字母-1个字节-8位,欧洲-2个字节-16位,中文-3个字节 ...
- python3.6调用c语言动态编译文件 c语言编译可执行文件和动态编译等
1.c的代码 dfunc.c #include<stdio.h> int dgfunc(int n) { ){ ; }else{ )+dgfunc(n-); } } 2.动态编译 cmd ...
- 用grok拆分java日志
1.假设一行日志内容如下: [root@VM_0_92_centos opt]# cat error.log -- ::,[ERROR ajp-nio--exec-](cn.com.al1.compo ...
- analyticdb(ADB) group by 用法小结
虽说analyticdb(ADB)是支持mysql协议的,但有些具体细节用法是有些区别. 1.group by 字段 mysql group by select id,title,describe w ...
- 一个SQL注释引发的线上问题
最近开始服务拆分,时间将近半个月.测试阶段也非常顺利,没有什么问题. 但上线之后的第二天,产品就风风火火的来找我们了,一看就是线上有什么问题.我们也不敢说,我们也不敢问,线上的后台商品忽然无法上架了, ...
- 10分钟学会web通讯的四种方式,短轮询、长轮询(comet)、长连接(SSE)、WebSocket
一般看到标题我们一般会产生下面几个问题??? 什么是短轮询? 什么是长轮询? 长连接又是什么? wensocket怎么实现呢? 他们都能实现web通讯,区别在哪呢,哪个好用呢? 接下来我们就一个个来了 ...
- Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...