ZZULI 21 Summer Final II

P1609 最小回文数

P1609 最小回文数

思路:

  • 模拟

具体实现:

  • 按照前半段统一,判断是否满足
  • 从中间开始 + 1 判断(注意9),是否满足,若不满足,则向两边扩展
  • 若都不能 + 1,全 9 的情况,直接输出答案 头尾为1中间为0,长度为原串+1

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define rf freopen("D://C.C++//C++_Project//sublime//in.txt", "r", stdin);
#define wf freopen("D://C.C++//C++_Project//sublime//out.txt", "w", stdout);

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define y1 Y1
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1e5 + 10, M = N * 2;
const int YB = 8, YM = 1e8;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int T, cases;
int n, m, k, times;

string s;

void solve()
{
	cin >> s;
	
	int len = s.size();
	int milen = len / 2;
	
	string temp = s;
	
	bool ones = false;
	int flag = 1;
	
	if(len & 1)
	{
		flag = 0;
		if(temp[milen] != '9')
		{
			++ temp[milen];
			ones = true;
		}
	}
	
	for (int i = 1; i <= milen; i ++ )
		temp[milen - i] = temp[milen + i - flag] = temp[milen - i];
		
	if(temp > s)
	{
		cout << temp << endl;
		return;
	}
	
	if(ones)
	{
		cout << temp << endl;
		return;
	}
	else 
	{
		int success = -1;
		
		for (int i = 1; i <= milen; i ++ )
		{
			if(temp[milen - i] != '9')
			{
				temp[milen - i] = temp[milen + i - flag] = ++ temp[milen - i];
				success = i;
				ones = true;
				break;
			}
		}
		
		if(success != -1)
		{
			if(len & 1) temp[milen] = '0';
			for (int i = 1; i < success; i ++ )
				temp[milen - i] = temp[milen + i - flag] = '0';
		}
	}
	
	if(ones)
	{
		cout << temp << endl;
		return;
	}
	else 
	{
		cout << 1;
		for (int i = 1; i <= len - 1; i ++ )
			cout << 0;
		cout << 1 << endl;
	}
	
    return;
}

signed main()
{
    //rf;wf;
    
    T = 1;
    fast;
	//cin >> T;
    //scanf("%d", &T);
    
    //for (cases = 1; cases <= T; cases ++ )
    while(T -- )
        solve();

    return 0;
}

P1165 日志分析

P1165 日志分析

思路:

具体实现:

  • 两个栈,一个维护当前序列的值
  • 一个维护,当前序列的最大值

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define rf freopen("D://C.C++//C++_Project//sublime//in.txt", "r", stdin);
#define wf freopen("D://C.C++//C++_Project//sublime//out.txt", "w", stdout);

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define y1 Y1
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1e5 + 10, M = N * 2;
const int YB = 8, YM = 1e8;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int T, cases;
int n, m, k, times;

stack<int> stk, mx;

void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i ++ )
	{
		int op, x;
		cin >> op;
		if(!op)
		{
			cin >> x;
			stk.push(x);
			if(mx.empty() || mx.top() <= x) mx.push(x);
		}
		else
		{
			if(op == 1)
			{
				int x = stk.top();
				stk.pop();
				if(x == mx.top()) mx.pop();
			}
			else
			{
				if(!mx.empty()) cout << mx.top() << endl;
				else cout << 0 << endl;
			}
		}
	}
	
	
    return;
}

signed main()
{
    //rf;wf;
    
    T = 1;
    fast;
	//cin >> T;
    //scanf("%d", &T);
    
    //for (cases = 1; cases <= T; cases ++ )
    while(T -- )
        solve();

    return 0;
}

P3718 [AHOI2017初中组]alter

P3718 [AHOI2017初中组]alter

思路:

  • 二分答案

具体实现:

  • mid 为最短长度,所以每mid段后必须+1,所以 res += w[i] / (mid + 1)
  • 特判 长度为 1 (两种情况取最小值)即可

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define rf freopen("D://C.C++//C++_Project//sublime//in.txt", "r", stdin);
#define wf freopen("D://C.C++//C++_Project//sublime//out.txt", "w", stdout);

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define y1 Y1
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1e5 + 10, M = N * 2;
const int YB = 8, YM = 1e8;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int T, cases;
int n, m, k, times;

int w[N];
vector<int> v;

bool check(int mid)
{
	int cnt = 0;
	for (int i = 0; i < v.size(); i ++ )
	{
		if(v[i] > mid)
			cnt += v[i] / (mid + 1);
	}
	
	return cnt <= m;
}

void solve()
{
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++ )
	{
		char ch;
		cin >> ch;
		if(ch == 'N') w[i] = 1;
		else w[i] = 0;
	}
	
	int last = w[1], cnt = 1;
	for (int i = 2; i <= n; i ++ )
	{
		if(w[i] != last)
		{
			v.push_back(cnt);
			last = w[i];
			cnt = 1;
		}
		else cnt ++;
	}
	if(cnt) v.push_back(cnt);
	
	int cnt1 = 0;
	last = 1;
	for (int i = 2; i <= n; i ++ )
	{
		if(w[i] != last) cnt1 ++;
		last ^= 1;
	}
	int cnt2 = 0;
	last = 0;
	for (int i = 2; i <= n; i ++ )
	{
		if(w[i] != last) cnt2 ++;
		last ^= 1;
	}
	
	if(min(cnt1, cnt2) <= m)
	{
		cout << 1 << endl;
		return;
	}
	
	int l = 2, r = n;
	while(l < r)
	{
		int mid = l + r >> 1;
		if(check(mid)) r = mid;
		else l = mid + 1;
	}
	
	cout << r << endl;
	
    return;
}

signed main()
{
    //rf;wf;
    
    T = 1;
    fast;
	//cin >> T;
    //scanf("%d", &T);
    
    //for (cases = 1; cases <= T; cases ++ )
    while(T -- )
        solve();

    return 0;
}

P1637 三元上升子序列

P1637 三元上升子序列

思路:

  • 树状数组

具体实现:

  • 树状数组维护给数前面后面的数

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define rf freopen("D://C.C++//C++_Project//sublime//in.txt", "r", stdin);
#define wf freopen("D://C.C++//C++_Project//sublime//out.txt", "w", stdout);

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define y1 Y1
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1e5 + 10, M = N * 2;
const int YB = 8, YM = 1e8;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int T, cases;
int n, m, k, times;

int w[N];
int l[N], r[N];
int tr[N];

int lowbit(int x)
{
	return x & -x;
}

void add(int x, int r)
{
	for (int i = x; i <= N; i += lowbit(i)) tr[i] += r;
}

int sum(int x)
{
	int res = 0;
	for (int i = x; i; i -= lowbit(i)) res += tr[i];
	return res;
}

void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i ++ )
		cin >> w[i];
	
	memset(tr, 0, sizeof tr);
	for (int i = 1; i <= n; i ++ )
	{
		int x = w[i];
		l[i] = sum(x - 1);
		add(w[i], 1);
	}
	
	memset(tr, 0, sizeof tr);
	for (int i = n; i >= 1; i -- )
	{
		int x = w[i];
		r[i] = sum(N) - sum(x);
		add(w[i], 1);
	}
	
	int res = 0;
	for (int i = 1; i <= n; i ++ )
		res += l[i] * r[i];
	
	cout << res << endl;
	
    return;
}

signed main()
{
    //rf;wf;
    
    T = 1;
    fast;
	//cin >> T;
    //scanf("%d", &T);
    
    //for (cases = 1; cases <= T; cases ++ )
    while(T -- )
        solve();

    return 0;
}

P1799 数列

P1799 数列

思路:

  • 线性DP

具体实现:

  • 见代码注释

代码如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>

#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define rf freopen("D://C.C++//C++_Project//sublime//in.txt", "r", stdin);
#define wf freopen("D://C.C++//C++_Project//sublime//out.txt", "w", stdout);

#define mkpr make_pair
#define endl '\n'
#define x first
#define y second
#define y1 Y1
#define int long long

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int mod = 998244353;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;

const int N = 1010, M = 1010;
const int YB = 8, YM = 1e8;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int T, cases;
int n, m, k, times;

int w[N];
int f[N][N];

// 状态表示:
// 集合:所有前i个数字中,已经用了j次删除的方案,含义为:所得的匹配数
// 属性:Max

// 状态计算:(考虑最后一步
// 若删除,f[i][j] = f[i - 1][j - 1]
// 若不删除i,f[i][j] = f[i - 1][j] + (a[i] == i - j);


void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i ++ )
		cin >> w[i];
	
	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j <= n; j ++ )
		{
			f[i][j] = max(f[i][j], f[i - 1][j] + (w[i] == i - j));
			if(j > 0) f[i][j] = max(f[i][j], f[i - 1][j - 1]);
		}
		
	int res = 0;
	for (int i = 0; i <= n; i ++ )
		res = max(res, f[n][i]);
	
	cout << res << endl;
	
    return;
}

signed main()
{
    //rf;wf;
    
    T = 1;
    fast;
	//cin >> T;
    //scanf("%d", &T);
    
    //for (cases = 1; cases <= T; cases ++ )
    while(T -- )
        solve();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AC自动寄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值