D. Not Adding
E. Not Escaping
F. Not Splitting
直接枚举就行了,原本还想的是素倍数,但是素倍数也不行。
bool dis[N];
int main()
{
int n, x;
scanf("%d", &n);
int ans = -n;
while(n --)scanf("%d", &x), dis[x] = 1;
for(int i = 1;i <= 1e6;i ++)
{
int t = 0;
for(int j = i;j <= 1e6; j += i)
if(dis[j])
t = __gcd(t, j);
ans += t == i;
}
cout<<ans<<endl;
return 0;
先分层,然后每层出入口的dp可以化简,因为是加法 (
∣
i
−
j
∣
∗
x
|i-j|*x
∣i−j∣∗x),那么去绝对值+两次遍历求出这层的每个点的最小花费。
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e6+10;
void chmi(LL &a, LL b){if(a > b)a = b;return ;}
struct point{int a, b, c, d, h;bool operator < (const point &b)const {return a < b.a;};};
LL x[N];
point a[N];
vector<int>v[N];
vector<LL>ans[N];
int pos(int i, int a){return lower_bound(v[i].begin(), v[i].end(), a)-v[i].begin();}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for(int i = 1;i <= n;i ++) scanf("%lld", x+i);
for(int i = 1;i <= k;i ++)
{
scanf("%d%d%d%d%d", &a[i].a, &a[i].b, &a[i].c, &a[i].d, &a[i].h);
v[a[i].a].push_back(a[i].b);
v[a[i].c].push_back(a[i].d);
}
a[0] = {n, m, n, m, 0};
sort(a, a+k+1);
a[k+1] = {0, 0, 0, 0, 0};// 楼梯的处理
v[1].push_back(1); v[n].push_back(m);
for(int i = 1;i <= n;i ++)
{
sort(v[i].begin(), v[i].end());
v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
for(int j = 0;j < v[i].size();j ++)
ans[i].push_back(1e18);
} // 离散化每层的点,可以存PII, 我存的是两个vector[]。代表点和最小花费的对应关系。
ans[1][0] = 0; // 每层的处理。
// 以上都是离散化相关操作
int l = 0, r = 0;
while(l <= k)
{
int now = a[l].a;
LL mi = 1e18;
for(int i = 0;i < v[now].size();i ++)
{
chmi(mi, ans[now][i]-v[now][i]*x[now]);
chmi(ans[now][i], mi+v[now][i]*x[now]);
}
mi = 1e18;
for(int i = v[now].size()-1;i >= 0;i --)
{
chmi(mi, ans[now][i]+v[now][i]*x[now]);
chmi(ans[now][i], mi-v[now][i]*x[now]);
} // 遍历两遍去绝对值。
while(a[r].a == a[l].a)
{
chmi(ans[a[r].c][pos(a[r].c, a[r].d)], ans[a[r].a][pos(a[r].a, a[r].b)] - a[r].h);
r ++;
} // 走起始点在这层的楼梯。
l = r;
}
ans[n].back() < 1e18/2 ? printf("%lld\n", ans[n].back()) : puts("NO ESCAPE"); // 1e18 也可能不太保险。
for(int i = 1;i <= n;i ++) v[i].clear(), ans[i].clear();
}
return 0;
}
额,他要对称,然后我想的是走两边dij,分别看左半和上半部分的最短路,权重就是经过把一对切掉的数量。
写的时候坎坎拌拌,一会儿k全局和局部冲突,一会儿map没脑子clear早了导致结果一直是n。这题不因该呀,难道是对称的证明是重点吗。
int ans[600][600], k, n; //
priority_queue<pair<int,PII>, vector<pair<int,PII>>, greater<pair<int,PII>>> p;
map<pair<PII, PII>, int> ma;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dij(int d, int r)
{
for(int i = 1;i <= d;i ++)
for(int j = 1;j <= r;j ++)
ans[i][j] = 0x3f3f3f3f;
ans[1][1] = 0;
p.push({0, {1, 1}});
while(p.size())
{
int s = p.top().first, x = p.top().second.first, y = p.top().second.second;
p.pop();
if(s > ans[x][y]) continue;
for(int i = 0;i < 4;i ++)
{
int xx = x+dx[i], yy = y+dy[i];
if(!xx || !yy ||xx > d || yy > r) continue;
if(ans[xx][yy] > ans[x][y] + ma[{{x,y}, {xx, yy}}])
// cout<<xx<<' '<<yy<<' '<<x<<' '<<y<<' '<<ma[{{x,y}, {xx, yy}}]<<endl,
p.push({ans[xx][yy] = ans[x][y] + ma[{{x,y}, {xx, yy}}], {xx, yy}});
}
}
return ans[k/2+1][k/2+1];
}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{ // 手欠多写了int n, k;
scanf("%d%d", &n, &k);
for(int i = 1;i <= n;i ++)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if(a == c)
ma[{{a, max(b, d)}, {a+1, max(b, d)}}] ++, ma[{{a+1, max(b, d)}, {a, max(b, d)}}] ++;
else
ma[{{max(a, c), b}, {max(a, c), b+1}}] ++, ma[{{max(a, c), b+1}, {max(a, c), b}}] ++;
a = 1+k-a, b = 1+k-b, c = 1+k-c, d = 1+k-d;
if(a == c)
ma[{{a, max(b, d)}, {a+1, max(b, d)}}] ++, ma[{{a+1, max(b, d)}, {a, max(b, d)}}] ++;
else
ma[{{max(a, c), b}, {max(a, c), b+1}}] ++, ma[{{max(a, c), b+1}, {max(a, c), b}}] ++;
}
// for(auto x : ma) cout<<x.first.first.first<<" "<<x.first.first.second<<' '<<x.first.second.first<<' '<<x.first.second.second<<' '<<x.second<<endl;
printf("%d\n", n-min(dij(k+1, k/2+1), dij(k/2+1, k+1)));
ma.clear(); // 放到printf前debug好长时间,,哎。
}
return 0;
}