第一题就暴力
第二题 芬兰木棋
被这题背刺了,最后才想明白wa点,想了一个小时,没时间写了,草了
当时贪心就拿了17分
下边是正解
用每个方向 gcd = 1 的坐标映射为这个方向,然后 gcd 作为 每个点在这个方向的坐标(也就是编号,id)因为一个方向上的点从原点出发有先后顺序,编号即可先后顺序。
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll n, m;
int x[maxn], y[maxn], p[maxn];
map<pair<int,int>,int> ma;
struct node
{
int id, p;
bool operator<(const node &B)const{
return id > B.id;
}
};
vector <node> v[maxn];
vector <node> xp,xn,yp,yyn;
int id;
int getans(vector <node> v, int ans = 0)
{
if(v.size() == 1) return 1;
sort(v.begin(), v.end());
v.push_back({inf, 0});
for(int i = 1; i < v.size(); ++i)
{
if(v[i].p == v[i-1].p && v[i].p == 1);
else ++ans;
}
return ans;
}
void work()
{
int n;
cin >> n;
ll ans = 0;
for(int i = 1; i <= n; ++i)
{
cin >> x[i] >> y[i] >> p[i];
ans += p[i];
if(!x[i])
{
if(y[i] > 0) yp.push_back({y[i], p[i]});
else yyn.push_back({-y[i], p[i]});
}
else if(!y[i])
{
if(x[i] > 0) xp.push_back({x[i], p[i]});
else xn.push_back({-x[i], p[i]});
}
else
{
int d = __gcd(abs(x[i]), abs(y[i]));
x[i] /= d; y[i] /= d;
if(!ma.count({x[i], y[i]})){
ma[{x[i], y[i]}] = ++id;
v[id].push_back({d, p[i]});
}
else v[ma[{x[i], y[i]}]].push_back({d, p[i]});
}
}
int cz = 0;
for(int i = 1; i <= id; ++i)
{
cz += getans(v[i]);
}
cz += getans(xp);
cz += getans(xn);
cz += getans(yp);
cz += getans(yyn);
cout << ans << " " << cz;
}
int main()
{
ios::sync_with_stdio(0);
//int TT;cin>>TT;while(TT--)
work();
return 0;
}
第三题 打怪升级
思路:
先用
F
l
o
y
e
d
Floyed
Floyed 算法求出全源最短路,然后暴力求出空降点
B
0
B_0
B0
注意空降的编号是考虑攻克所有点,而不仅仅是查询的点
然后跑一遍
B
0
B_0
B0 点的最短路
最短路可能不止一条,因此也结合了最短路计数这道题
不会计数可以做做这道题
用
p
r
e
pre
pre 记录路径,更新最短路的时候顺便更新路径和武器价值即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int maxn = 1e3 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll n, m;
int dis[maxn][maxn], Dis[maxn];
int val[maxn];
struct node
{
int to, next, val, eng;
}e[maxn*maxn];
int head[maxn], cnt, vis[maxn];
int pre[maxn];
inline void add(int x, int y, int z, int p)
{
e[++cnt].to = y;
e[cnt].eng = z;
e[cnt].val = p;
e[cnt].next = head[x];
head[x] = cnt;
}
void dij(int s)
{
memset(Dis, 0x3f, sizeof(Dis));
Dis[s] = 0;
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > > q;
q.push({0, s});
while(!q.empty())
{
int now = q.top().second;q.pop();
if(vis[now]) continue;
vis[now] = 1;
for(int i = head[now]; i; i = e[i].next)
{
int to = e[i].to;
if(Dis[to] > Dis[now] + e[i].eng)
{
Dis[to] = Dis[now] + e[i].eng;
val[to] = val[now] + e[i].val;
pre[to] = now;
q.push({Dis[to], to});
}
else if(Dis[to] == Dis[now] + e[i].eng)
{
if(val[to] < val[now] + e[i].val){
val[to] = val[now] + e[i].val;
pre[to] = now;
}
}
}
}
}
void print(int st, int en)
{
if(st == en) cout << st << endl;
else
{
int j = en;
stack <int> s;
while(j != st)
{
s.push(j);
j = pre[j];
}
s.push(st);
while(!s.empty()){
cout << s.top();s.pop();
if(s.size()) cout << "->";
else cout << endl;
}
}
}
void work()
{
cin >> n >> m;
memset(dis, 0x3f, sizeof(dis));
for(int i = 1; i <= n; ++i) dis[i][i] = 0;
for(int i = 1; i <= m; ++i)
{
int x, y, a, b;cin >> x >> y >> a >> b;
add(x, y, a, b);add(y, x, a, b);
dis[x][y] = dis[y][x] = a;
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
vector <int> v;
int k;cin >> k;
for(int i = 0; i < k; ++i)
{
int p;cin >> p;
v.push_back(p);
}
int Min = inf, pos;
for(int i = n; i >= 1; --i)
{
int Max = 0, po;
for(int j = 1; j <= n; ++j) if(j != i)
{
if(dis[i][j] > Max) Max = dis[i][j];
}
if(Min >= Max) Min = Max, pos = i;
}
cout << pos << endl;
dij(pos);
for(int i = 0; i < v.size(); ++i)
{
print(pos, v[i]);
cout << Dis[v[i]] << " " << val[v[i]] << endl;
}
}
int main()
{
ios::sync_with_stdio(0);
//int TT;cin>>TT;while(TT--)
work();
return 0;
}
第四题
建图,暴搜,特判点起点终点就能拿22分
正解是思维+并查集
正向是删点的过程,比较麻烦,难处理
考虑离线查询
倒着遍历查询就是加点的过程,这个过程就很舒服了
用并查集维护连通性即可
注意要先查询答案再更新加点,还有就是要先更新掉没有被删掉的点,再进行离线查询
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
ll n, m;
int f[maxn];
vector <int> v[maxn];
struct node
{
int x, y;
vector <int> a;
vector <int> b;
}l[1009];
int Find(int x)
{
return x == f[x] ? x : f[x] = Find(f[x]);
}
int pos[maxn], vis[maxn];
void work()
{
int d;cin>>n>>m>>d;
for(int i = 1; i <= n; ++i) f[i] = i;
for(int i = 1; i <= m; ++i)
{
int x, y;cin >> x >> y;
v[x].push_back(y);v[y].push_back(x);
}
for(int i = 1; i <= d; ++i)
{
cin >> l[i].x >> l[i].y;
pos[l[i].x] = 1;
for(int j = 1; j <= l[i].y; ++j)
{
int u, v;cin >> u >> v;
l[i].a.push_back(u);
l[i].b.push_back(v);
}
}
for(int i = 1; i <= n; ++i) if(!pos[i])// 先更新没有被删的点
{
int u = Find(i);
for(int j = 0; j < v[i].size(); ++j)
{
if(pos[v[i][j]]) continue;
int p = Find(v[i][j]);
if(p !=u) f[p] = u;
}
}
stack<int> s;
for(int i = d; i >= 1; --i)
{
int ans = 0;
for(int j = 0; j < l[i].y; ++j)
{
int xx = Find(l[i].a[j]);
int yy = Find(l[i].b[j]);
if(xx != yy) ++ans;
}
int u = l[i].x;
pos[u] = 0;
int xx = Find(u);
for(int j = 0; j < v[u].size(); ++j)
{
if(pos[v[u][j]]) continue;
int yy = Find(v[u][j]);
if(xx != yy){
f[yy] = xx;
}
}
s.push(ans);
}
while(!s.empty())
{
cout << s.top();
if(s.size() != 1) cout << endl;
s.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
//int TT;cin>>TT;while(TT--)
work();
return 0;
}
/*
5 5 3
1 2
1 3
1 5
2 5
3 4
4 3
1 3
1 4
2 3
5 3
3 4
2 3
3 5
1 3
2 3
2 5
3 4
*/