Educational Codeforces Round 87 (Rated for Div. 2)
A
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll a,b,c,d;
int t;
scanf("%d",&t);
while(t--){
cin>>a>>b>>c>>d;
if(a<=b){
printf("%lld\n",b);
continue;
}
if(c<=d){
printf("-1\n");
continue;
}
ll ans;
ll temp = (a-b)/(c-d);
if((a-b)%(c-d)!=0)temp++;
ans = b + temp*c;
cout<<ans<<endl;
}
}
B
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
int t;
char s[N];
int num = 0;
int st[N];
int cnt[N];
int main()
{
t = read();
while (t--)
{
num = 0;
scanf("%s", s + 1);
int n = strlen(s + 1);
upd(i, 1, n)cnt[i] = 0, st[i] = 0;
int one = 0, two = 0, thr = 0;
upd(i, 1, n)
{
if (s[i] == '1')one++;
else if (s[i] == '2')two++;
else thr++;
}
if (!one || !two || !thr)
{
printf("%d\n", 0);
}
else {
upd(i, 1, n)
{
if (num == 0)
{
st[++num] = s[i];
}
else {
if (st[num] != s[i])
{
st[++num] = s[i];
cnt[num] = 1;
}
else {
cnt[num] ++;
}
}
}
int ans = INF;
upd(i, 2, num - 1)
{
if (st[i - 1] != st[i + 1])
{
ans = min(ans, cnt[i] + 2);
}
}
printf("%d\n", ans);
}
}
return 0;
}
C1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int n;
int t;
scanf("%d",&t);
while(t--) {
scanf("%d", &n);
double pi = acos(-1);
double f = pi * 0.5 / n;
double ans = 1 / tan(f);
printf("%.9lf\n", ans);
}
}
D 板子
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e6 + 10;
int n, q;
int tree[N << 2];
void update(int l, int r, int root, int pos, int val)
{
if (l == r)
{
tree[root] += val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)update(lson, pos, val);
else update(rson, pos, val);
tree[root] = tree[lrt] + tree[rrt];
}
int query(int l, int r, int root, int k)
{
if (l == r)
{
return l;
}
int mid = (l + r) >> 1;
if (tree[lrt] >= k)
{
return query(lson, k);
}
else return query(rson, k - tree[lrt]);
}
int query2(int l, int r, int root)
{
if (l == r)
{
return l;
}
int mid = (l + r) >> 1;
if (tree[lrt])
{
return query2(lson);
}
else return query2(rson);
}
int main()
{
n = read(), q = read();
int x;
upd(i, 1, n) {
x = read();
update(1, n, 1, x, 1);
}
int kk;
while (q--)
{
kk = read();
if (kk >= 0)
{
update(1, n, 1, kk, 1);
}
else {
kk = abs(kk);
int temp = query(1, n, 1, kk);
update(1, n, 1, temp, -1);
}
}
if (tree[1] == 0)
{
cout << 0 << endl;
}
else {
cout << query2(1, n, 1);
}
return 0;
}
E
化成二分图后,利用背包解决问题。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 5e3 + 10;
vector<int>vec[N];
vector<vector<int> >scc;
pir pp[N];
int col[N];
int n, m;
int dp[N][2 * N];
int num[4];
int tot_one, tot_two;
bool flag = 0;
int ans[N];
int scc_cnt = 0;
void dfs(int u, int c)
{
col[u] = c;
scc[scc_cnt].push_back(u);
if (c == 0)
tot_one++;
else tot_two++;
for (auto k : vec[u])
{
if (col[k] == -1)
{
dfs(k, c ^ 1);
}
else if (col[k] == col[u])
{
flag = 1;
}
}
}
int main()
{
n = read(); m = read();
upd(i, 1, 3)num[i] = read();
int u, v;
upd(i, 1, m)
{
u = read(), v = read();
vec[u].push_back(v);
vec[v].push_back(u);
}
scc.push_back(vector<int>());
memset(col, -1, sizeof(col));
upd(i, 1, n)
{
if (col[i] == -1)
{
tot_one = tot_two = 0;
++scc_cnt;
scc.push_back(vector<int>());
dfs(i, 0);
if (flag)
{
cout << "NO" << endl;
return 0;
}
pp[scc_cnt] = make_pair(tot_one, tot_two);
}
}
dp[0][0] = 1;
upd(i, 1, scc_cnt)
{
upd(j, 0, num[2])
{
if (dp[i - 1][j])
{
dp[i][j + pp[i].first] = 1;
dp[i][j + pp[i].second] = 1;
}
}
}
if (!dp[scc_cnt][num[2]])
{
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
int tempnum = num[2];
dwd(i, scc_cnt, 1)
{
if (tempnum>=pp[i].first&&dp[i-1][tempnum-pp[i].first]) {
tempnum -= pp[i].first;
for (auto k : scc[i])
{
if (col[k] == 1)
{
if (num[1])
{
ans[k] = 1; num[1]--;
}
else {
ans[k] = 3; num[3]--;
}
}
else {
ans[k] = 2;
}
}
}
else {
tempnum -= pp[i].second;
for (auto k : scc[i])
{
if (col[k] == 0)
{
if (num[1])
{
ans[k] = 1; num[1]--;
}
else {
ans[k] = 3; num[3]--;
}
}
else {
ans[k] = 2;
}
}
}
}
upd(i, 1, n)printf("%d", ans[i]);
}
return 0;
}
F(dp做法,二分图带权匹配也可以)对bi贪心排序后,就是一个正常的dp了。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 110;
int T, n, k;
struct node {
ll a, b;
int id;
bool operator<(const node temp)const {
return b < temp.b;
}
}nd[N];
ll dp[N][N];
int pre[N][N];
int main()
{
T = read();
while (T--)
{
n = read(), k = read();
upd(i, 0, n)upd(j, 0, k)dp[i][j] = -1e18, pre[i][j] = 0;
upd(i, 1, n)
{
nd[i].a = read(), nd[i].b = read(); nd[i].id = i;
}
sort(nd + 1, nd + n + 1);
dp[0][0] = 0;
//upd(i, 1, n)cout << nd[i].a << nd[i].b << nd[i].id << endl;
upd(i, 1, n)
{
upd(j, 0, k)
{
if (j > i)break;
if (j >= 1&&dp[i-1][j-1]!=-INF)
{
dp[i][j] = dp[i - 1][j - 1] + nd[i].a + (j - 1)*nd[i].b;
pre[i][j] = j - 1;
}
if (dp[i - 1][j]!=-INF)
{
ll temp = dp[i - 1][j] + (k - 1)*nd[i].b;
if (temp > dp[i][j])
{
dp[i][j] = temp;
pre[i][j] = j;
}
}
}
}
vector<int>ans;
vector<int>ans2;
int kk = k;
dwd(i, n, 1)
{
if (pre[i][kk] == kk)
{
ans2.push_back(-nd[i].id); ans2.push_back(nd[i].id);
}
else {
ans.push_back(nd[i].id);
kk = pre[i][kk];
}
}
reverse(ans.begin(), ans.end());
reverse(ans2.begin(), ans2.end());
cout << ans.size() + ans2.size() << endl;
up(i,0,ans.size()-1)
{
printf("%d ", ans[i]);
}
for (auto k : ans2)
printf("%d ", k);
printf("%d\n", ans.back());
}
}
G
三步查询,玄学查询第一个是否是最重的,倍增查询第一个gift所在区间,找到区间二分查询。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
int T, n, k;
char s[100];
int main()
{
srand((int)time(0));
T = read();
while (T--)
{
n = read(), k = read();
bool flag = 0;
upd(i, 1, 30)
{
int pos = 0;
while (!pos)pos = rand() % n;
printf("? 1 1\n");
printf("1\n%d\n", pos + 1);
fflush(stdout);
scanf("%s", s);
if (s[0] == 'F'||s[0]=='E')
{
continue;
}
else {
flag = 1;
break;
}
}
if (flag)
{
printf("! 1\n");
fflush(stdout);
}
else {
int pos = 1;
for (int i = 1; i*2 <= n; (i <<= 1),(pos<<=1))
{
printf("? %d %d\n", i, i);
upd(j, 1, i)cout << j << " ";cout << endl;
upd(j, 1, i)cout << i+j << " "; cout << endl;
fflush(stdout);
scanf("%s", s);
if (s[0] == 'F')
{
break;
}
}
int lf = pos; int rt = min(n + 1, pos * 2 + 1);
while (rt - lf > 1)
{
int mid = (rt + lf) >> 1;
int len = mid - lf;
printf("? %d %d\n", len, len);
upd(i, 1, len)cout << i << " "; cout << endl;
upd(i, lf + 1, mid)cout << i << " "; cout << endl;
fflush(stdout);
scanf("%s", s);
if (s[0] == 'F')
{
rt = mid;
}
else {
lf = mid;
}
}
printf("! %d\n", rt);
fflush(stdout);
}
}
return 0;
}
橙橙橙