Codeforces 946 课程表背包DP 数位DFS构造
A
B
给你A,B 两个数 1.a=0 OR b=0 break 2.a>=2b a=a-2b 3.b>=2a b=b-2a
如果只是单纯模拟肯定会超时
只需要简化 a>=2b --> a%=2b b>=2a --> b%=2a就可以
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-10;
const double EPS = 1.0e-4;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int main()
{
ll a, b;
cin >> a >> b;
int flag = ;
while (!flag)
{
ll moda = 1LL * * a;
ll modb = 1LL * * b;
if (a == || b == )
{
break;
}
if (a >= 1LL * * b)
{
a = a % modb;
continue;
}
else
{
if (b >= 1LL * * a)
{
b = b % moda;
continue;
}
else
{
break;
}
}
//cout << a << " " << b << endl;
}
cout << a << " " << b << endl;
}
C
子序列和子串要分清楚。。
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-10;
const double EPS = 1.0e-4;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 3e7;
int num[];
int zimu[];
int len;
int flag=;
void dfs(int x, int now)
{
if(now==)
{
flag=;
return;
}
for (int i = x; i <= len; i++)
{
if (num[i] <= now)
{
num[i] = now;
dfs(i + , now + );
break;
}
}
}
int main()
{
for (int i = ; i <= ; i++)
{
zimu[i] = i;
}
string a;
cin >> a;
flag = ;
len = a.size();
if (len <= )
{
cout << - << endl;
return ;
}
for (int i = ; i < len; i++)
{
num[i + ] = a[i] - 'a';
}
dfs(, );
if (flag)
{
for (int i = ; i <= len; i++)
{
char ch = 'a';
ch += num[i];
cout << ch;
}
}
else
{
cout << - << endl;
}
}
D
贪心不可做 正解是背包DP
其中dp[i][j]表示前 i 天删去 j 节课所需要的最少上课时间
dp方程是 dp[i][j+k]=min(dp[i][j+k],dp[i-1][j]+ke[k]) 其中ke[i]表示的是当天除去i节课所需要最少上课时间
ke[i]需要用前缀和来算
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-10;
const double EPS = 1.0e-4;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
const int maxn = ;
int pre[maxn];
char f[maxn];
int ke[maxn];
int dp[maxn][maxn];
int main()
{
int n, m, K;
cin >> n >> m >> K;
for (int i = ; i <= n; i++)
{
mem(dp[i], 0x3f3f3f3f);
mem(ke, 0x3f3f3f3f);
scanf("%s", f + );
for (int j = ; j <= m; j++)
{
pre[j] = pre[j - ] + (f[j] == '');
}
if (pre[m] <= K)
{
ke[pre[m]] = ;
}
for (int j = ; j <= m; j++)
{
for (int k = j; k <= m; k++)
{
int len = pre[m] - pre[k] + pre[j - ];
if (len <= K)
{
ke[len] = min(ke[len], k - j + );
}
}
}
for (int j = ; j <= K; j++)
{
for (int k = ; j + k <= K; k++)
{
dp[i][j + k] = min(dp[i][j + k], dp[i-][j] + ke[k]);
}
}
}
cout << dp[n][K] << endl;
}
E
给你一个数字,求一个比当前数字小的 最大的漂亮数字
(漂亮数:所以在当前数中出现的数出现的次数为偶数次)
当长度为奇数时 答案明显是len-1个9
偶数时就需要搜索 找出最后面的字典序可以比原序列小的地方
如果空格数大于当前出现次数为奇数的数的个数就输出9
因为整体是偶数个数 0-9都是构造的成双出现的 所以一定满足
如果找不到 就输出len-2个9; (小的数例如10不用特殊处理 因为题目保证答案一定存在)
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que; get min
const double eps = 1.0e-10;
const double EPS = 1.0e-4;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
const int maxn = 2e5 + ;
int cnt[maxn][];
char f[maxn];
int n;
int len;
void getans()
{
int i, j, k;
for (i = len; i >= ; i--)
{
for (j = (int)(f[i] - '') - ; j >= (i == ); j--)
{
int now = ;
for (k = ; k <= ; k++)
{
now += cnt[i - ][k] ^ (k == j);
}
if (now <= len - i)
{
for (k = ; k < i; k++)
{
cout << f[k];
}
cout << j;
for (k = ; k <= len - i - now; k++)
{
cout << ;
}
for ( k = ; k >= ; k--)
{
if (cnt[i - ][k] ^ (k == j))
{
cout << k;
}
}
cout << endl;
return ;
}
}
}
for (i = ; i <= len - + (len % ); i++)
{
cout << ;
}
cout << endl;
}
int main()
{
cin >> n;
while (n--)
{
scanf("%s", f + );
len = strlen(f + );
for (int i = ; i <= ; i++)
{
cnt[][i] = ;
}
for (int i = ; i <= len; i++)
{
int now = f[i] - '';
for (int j = ; j <= ; j++)
{
cnt[i][j] = cnt[i - ][j] ^ (j == now);
}
}
getans();
}
}
Codeforces 946 课程表背包DP 数位DFS构造的更多相关文章
- codeforces 681D Gifts by the List dfs+构造
题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- 【bzoj4182】Shopping 树的点分治+dfs序+背包dp
题目描述 给出一棵 $n$ 个点的树,每个点有物品重量 $w$ .体积 $c$ 和数目 $d$ .要求选出一个连通子图,使得总体积不超过背包容量 $m$ ,且总重量最大.求这个最大总重量. 输入 输入 ...
- Codeforces 730J:Bottles(背包dp)
http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...
- P1021 邮票面值设计(dfs+背包dp)
P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大 ...
- codeforces 401D. Roman and Numbers 数位dp
题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. #include<bits/stdc++. ...
随机推荐
- Js自定义数组map之bigmap
/** * like map,but prev,curr,next and index will be given * @param {Function} cb Callback,the parame ...
- 64位 Qt5.12 MySql 连接问题
关于怎么检查Qt是否带MySql驱动 ,到Qt安装目录下 plugins\sqldrivers下寻找是否有qsqlmysql.dll文件 例如:F:\Qt\Qt5.9.6\5.9.6\msv ...
- qemu-kvm: unable to map backing store for guest RAM: Cannot allocate memory
当给 KVM 虚拟机设置 hugepage 时,需要在虚拟机的配置文件里加上下面一段: <memoryBacking> <hugepages/></memoryBacki ...
- hibernate映射简单实例
1创建数据库: --班级表 create table grade ( gid number primary key, --班级ID gname varchar2(50), --班级名称 gdesc v ...
- Go Int转string几种方式性能测试
Go Int转string几种方式性能测试 - 贤冰的博客 - CSDN博客 https://blog.csdn.net/flyfreelyit/article/details/79701577
- freemarker程序开发
1.程序开发入门 1.1 创建配置实例 首先,你应该创建一个freemarker.template.Configuration的实例,然后调整它的设置.Configuration实例是存储FreeMa ...
- 【ABAP系列】SAP 如何用ABAP实现自动发送外部邮件
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 如何用ABAP实现自动发 ...
- Java与C#不同
1.C#方法定义可以有默认参数,而Java则不支持该方式. C#方法定义 public void ShowMessage(string text,string orderId="" ...
- 【HBase】二、HBase实现原理及系统架构
整个Hadoop生态中大量使用了master-slave的主从式架构,如同HDFS中的namenode和datanode,MapReduce中的JobTracker和TaskTracker,YAR ...
- 操作系统 - Linux操作系统 - Centos - Centos7 - 安装|命令|使用汇总
镜像: http://mirrors.aliyun.com/centos/7/isos/x86_64/http://archive.kernel.org 网络配置 - DHCP # /etc/res ...