Educational Codeforces Round 104 (Rated for Div. 2)
A. Arena
int a[maxn];
int main()
{
ios;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int minn=inf;
for(int i=1;i<=n;i++) { cin >> a[i]; minn=min(minn,a[i]);}
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=minn) ans++;
}
cout << ans << endl;
}
}
B. Cat Cycle
int a[maxn];
int main()
{
ios;
int t;
cin >> t;
while (t--) {
ll n,k;
cin >> n >> k;
if(n%2==0) { cout << (k%n==0?n:k%n) << endl; continue;}
ll cir=(n-1)/2;
k%=(cir*n);
if(k==0) k=cir*n;
ll pos=((k%n==0?n:k%n)+(k-1)/cir)%n==0?n:((k%n==0?n:k%n)+(k-1)/cir)%n;
cout << pos << endl;
}
}
C. Minimum Ties
构造
- 如果n为奇数则-1,1,-1…轮流输出即可
- 如果n为偶数,对于team1而言,前(n+1)/2-1个数输出1,中间第(n+1)/2数输出0,后(n+1)/2-1个数输出-1,合计(n-1)个数。team2就是输出team1前(n-2)个数,依次列推。。。team(n-1)输出team1前1个数即可。
int l[maxn];
int main()
{
ios;
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
if(n%2)
{
for(int i=1;i<=n*(n-1)/2;i++)
{
if(i%2) cout << -1 << ' ';
else cout << 1 << ' ';
}
cout << endl;
continue;
}
else {
for(int i=1;i<=n-1;i++)
{
if(i<(n+1)/2) l[i]=1;
else if(i==(n+1)/2) l[i]=0;
else l[i]=-1;
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=n-i;j++) cout << l[j] << ' ';
}
cout << endl;
}
}
}
D. Pythagorean Triples
打表
vector<ll> a;
int main()
{
ios;
for(ll i=1;i<=25000;i++) a.pb(2*i*i+2*i+1);
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
int ans=upper_bound(a.begin(), a.end(), n)-a.begin();
cout << ans << endl;
}
}
E. Cheap Dinner
线段树维护dp数组
d p [ i ] [ j ] : 第 i 种 菜 品 选 第 j 个 菜 时 的 最 小 花 费 {dp[i][j]:第i种菜品选第j个菜时的最小花费} dp[i][j]:第i种菜品选第j个菜时的最小花费
维护的每一层dp数组用线段树表示(很明显本题需要维护四个菜品线段树),递推时当前菜品与上一种不能匹配的菜将上一种菜品划分为一个个区间,用线段树维护区间最小值并进行递推达到用线段树维护dp数组。
线段树之间的递推转移就是(上一层区间之间取最小值+当前这个数的权值)来实现单点更新。
最后的答案就是最后一个线段树整个区间最小值就是答案。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+5e4+10;
const int maxm=2e5+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n[5],m[5];
int a[5][maxn],Q[maxn];
int tree[5][maxn<<2],addv[5][maxn<<2];
void pushup(int num,int p) { tree[num][p]=min(tree[num][p*2+1],tree[num][p*2]); }
void build(int num,int p,int l,int r)
{
addv[num][p]=inf;
if(l==r) { tree[num][p]=Q[l]; return ;}
int mid=(l+r)/2;
build(num, p*2, l, mid);
build(num, p*2+1, mid+1, r);
pushup(num,p);
}
void pushdown(int num,int p)
{
if(addv[num][p]==inf) return;
addv[num][p*2]=min(addv[num][p*2],addv[num][p]);
addv[num][p*2+1]=min(addv[num][p*2+1],addv[num][p]);
tree[num][p*2]=min(addv[num][p],tree[num][p*2]);
tree[num][p*2+1]=min(addv[num][p],tree[num][p*2+1]);
addv[num][p]=inf;
}
void update(int num,int p,int l,int r,int ul,int ur,int v)
{
if(ul>ur) return;
if(ul<=l && r<=ur)
{
addv[num][p]=min(addv[num][p],v);
tree[num][p]=min(tree[num][p],v);
return ;
}
pushdown(num, p);
int mid=(l+r)/2;
if(ul<=mid) update(num, p*2, l, mid, ul, ur, v);
if(ur>mid) update(num, p*2+1, mid+1, r, ul, ur, v);
pushup(num, p);
}
int query(int num,int p,int l,int r,int ql,int qr)
{
if(ql>qr) return inf;
if(ql<=l && r<=qr) return tree[num][p];
int mid=(l+r)/2;
pushdown(num, p);
int ans=inf;
if(ql<=mid) ans=min(ans,query(num, p*2, l, mid, ql, qr));
if(qr>mid) ans=min(ans,query(num, p*2+1, mid+1, r, ql, qr));
pushup(num, p);
return ans;
}
int main()
{
ios;
for(int i=1;i<=4;i++) cin >> n[i];
for(int i=1;i<=4;i++)
{
for(int j=1;j<=n[i];j++)
{
cin >> a[i][j];
if(i==1) Q[j]=a[i][j];
else Q[j]=inf;
}
build(i, 1, 1, n[i]);
}
for(int i=2;i<=4;i++)
{
vector<int> g[maxn];
cin >> m[i];
for(int j=0;j<m[i];j++)
{
int x,y; cin >> x >> y;
g[y].push_back(x);
}
for(int j=1;j<=n[i];j++)
{
if(g[j].size()==0)
{
int tmp=query(i-1, 1, 1, n[i-1], 1, n[i-1]);
if(tmp==inf) continue;
update(i, 1, 1, n[i], j,j, tmp+a[i][j]);
}
else {
sort(g[j].begin(), g[j].end());
g[j].push_back(n[i-1]+1);
int pre=0;
for(int k=0;k<g[j].size();k++)
{
int v=g[j][k];
int tmp=query(i-1, 1, 1, n[i-1], pre+1, v-1);
pre=v;
if(tmp==inf) continue;
update(i, 1, 1, n[i], j,j, tmp+a[i][j]);
}
}
}
}
int ans=query(4, 1, 1, n[4], 1, n[4]);
if(ans==inf) cout << -1 << endl;
else cout << ans << endl;
}