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++. ...
随机推荐
- 启动Maven项目时报错Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project **-web: Failed to clean project: Failed to delete E:\**\target\tomcat\logs\access_lo
这类错误 出现这种错误,通常是由于您已启动了另一个tomcat 进程或者运行的javaw.exe进程,导致报错. 解决方法: 1. 鼠标点击 X 进行关闭运行失败的 Console页,(如果运行多次, ...
- 提示标签title折行
使用代码换行共两种代码,均可实现html标签内title内容显示时换行.换行代码符分别为:“ ”和“ ”以上符合数字输入均必须英文半角模式输入.使用时候,在需要换行地方任选一种(组)换行符号代码即可.
- VGA接口一根针折了
注意!!要由 针 对照着 接口 看!!别看反了! VGA接头图如下: VGA接口,15根针,其对应接口定义如下: 1红基色 red 2 绿基色 green 3 蓝基色 blue 4 地址码 ID Bi ...
- 设计模式(5): vue 不监听绑定的变量
概述 最近最近做项目的时候总会思考一些大的应用设计模式相关的问题,我把自己的思考记录下来,供以后开发时参考,相信对其他人也有用. 绑定变量 一般情况下,如果我们需要在组件中使用某个变量,会这么使用: ...
- spring的组件工厂后置处理器——BeanFactoryPostProcessor
作用和调用时机 spring有两种后置处理器: 1. 组件后置处理器——org.springframework.beans.factory.config.BeanPostProcessor: 2. 工 ...
- 全局namespace与模块内的namespace
declare global{ declare namespace xxx } 相当于 在一个js文件的顶级部分 declare namespace xxx 声明的都是全局的namespace, 如果 ...
- Linx下Keepalived做成服务
在/usr目录下面执行: find -name keepalived 返回如下: ./sbin/keepalived ./local/sbin/keepalived ./local/etc/keepa ...
- 应用安全 - harbaor - 漏洞汇总
CVE-2019-19026(SQL注入,高危):https://github.com/goharbor/harbor/security/advisories/GHSA-rh89-vvrg-fg64( ...
- maven spark Scala idea搭建maven项目的 pom.xml文件配置
1.pom.xml文件配置,直接上代码. <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- 学了一天的golang从入门到放弃
Google的go就是个二货,不实用,它最多只能和c比简单.low!