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构造的更多相关文章

  1. codeforces 681D Gifts by the List dfs+构造

    题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...

  2. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  3. Codeforces Gym 100418J Lucky tickets 数位DP

    Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  4. 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/ ...

  5. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  6. 【bzoj4182】Shopping 树的点分治+dfs序+背包dp

    题目描述 给出一棵 $n$ 个点的树,每个点有物品重量 $w$ .体积 $c$ 和数目 $d$ .要求选出一个连通子图,使得总体积不超过背包容量 $m$ ,且总重量最大.求这个最大总重量. 输入 输入 ...

  7. Codeforces 730J:Bottles(背包dp)

    http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...

  8. P1021 邮票面值设计(dfs+背包dp)

    P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大 ...

  9. codeforces 401D. Roman and Numbers 数位dp

    题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. #include<bits/stdc++. ...

随机推荐

  1. Spring4配置文件模板

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. @清晰掉 c语言三"巨头" const:volatile:static

    const: 1.如果把const放在变量类型前,说明这个变量的值是保持不变的(即为常量),改变量必须在定义时初始化,初始化后对她的任何赋值都是非法的. 2.当指针或是引用指向一个常量时,必须在类型名 ...

  3. 尚硅谷Docker---6-10、docker的安装

    尚硅谷Docker---6-10.docker的安装 一.总结 一句话总结: docker的安装使用非常简单,安装的话yum安装epel和docker,使用的话就是docker run命令 1.doc ...

  4. 各种sort排序总结

    冒泡排序 选择排序 插入排序

  5. Microsoft:Team Foundation Server 20XX Release Notes

    ylbtech-Microsoft:Team Foundation Server 2017 Release Notes 1.返回顶部 1. https://docs.microsoft.com/en- ...

  6. numpy库简单使用

    numpy简介 NumPy(Numerical Python)是python语言的一个扩展程序库,支持大量维度数组与矩阵运算,此外,也针对数据运算提供大量的数学函数库. NumPy是高性能科学计算和数 ...

  7. CentOS7 yum报 Cannot retrieve metalink for repository: epel/x86_64. Please verify its path解决方法

    打开/etc/yum.repos.d/epel.repo,将 [epel] name=Extra Packages for Enterprise Linux 6 – $basearch baseurl ...

  8. Scala的to和until

    object test03 { def main(args: Array[String]): Unit = { //to 每次迭代为1 val to1= to print("to1" ...

  9. 本机添加DNS映射

    开发项目时本地进行代码编写测试,需要与服务器主机进行DNS映射. 本地主机添加DNS映射步骤 一:复制备份hosts文件 找到C:\Windows\System32\drivers\etc下的host ...

  10. django连接数据库的类型

    字段类型 django的models里面字段类型除了上面的常用的 models.CharField和models.IntegerField,还有更多的类型 1.models.AutoField 自增列 ...