Codeforces 922 思维贪心 变种背包DP 质因数质数结论
A
#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-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e7 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll mod = 1e9 + ;
int main()
{
int x, y;
cin >> x >> y;
if (x == || y == )
{
if (x == && y == )
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return ;
}
int cur = y - ;
int remain = x - cur;
if (remain % == && cur && remain>=)
{
cout << "Yes" << endl;
return ;
}
cout << "No" << endl;
return ;
}
B
#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 turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll Mod = ;
int num[];
int cnt;
bool check(int a, int b, int c)
{
if (a + b > c && a + c > b && b + c > a)
{
return true;
}
return false;
}
int main()
{
int n;
int anser = ;
cin >> n;
for (int i = ; i < n; i++)
{
for (int j = i + ; j <= n; j++)
{
int aim = (i ^ j);
if (aim >= j && aim <= n && check(i, j, aim))
{
//cout << i << " " << j << " " << aim << endl;
anser++;
}
}
}
cout << anser << endl;
return ;
}
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;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxn = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
const int turn2[][] = {{, }, { -, }, {, }, {, -}, {, -}, { -, -}, {, }, { -, }};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
int main()
{
//freopen("sample.txt", "w", stdout);
ll n, k;
cin >> n >> k;
if (n == && k <= )
{
cout << "Yes" << endl;
return ;
}
if (k >= n)
{
cout << "No" << endl;
}
else
{
for (ll i = ; i <= min(1LL * , k); i++)
{
if (n % i != i - )
{
cout << "No" << endl;
return ;
}
}
cout << "Yes" << endl;
}
return ;
}
D
奇葩贪心题 假设有两个字符串A B A在B前面的条件是什么?
假设A有SHa个sh BSHb A有Sa个s BSb A有Ha个h BHb 当 SHa+SHb+Sa*Hb>SHa+SHb+Sb*Ha 时成立
转换一下就是A的sh比例比B高的时候 A在B前面
#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;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxn = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
const int turn2[][] = {{, }, { -, }, {, }, {, -}, {, -}, { -, -}, {, }, { -, }};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
struct node
{
string str;
double per;
} s[];
bool operator <(node a, node b)
{
return a.per > b.per;
}
ll pre[];
string aim = "";
int main()
{
ll anser = ;
//freopen("sample.txt", "w", stdout);
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
int now = ;
cin >> s[i].str;
int len = s[i].str.size();
for (int j = ; j < len; j++)
{
if (s[i].str[j] == 's')
{
now++;
}
}
if (len - now == )
{
s[i].per = ;
}
else
{
s[i].per = (double)now / (double)(len - now);
}
//cout << s[i].per << endl;
}
sort(s + , s + + n);
for (int i = ; i <= n; i++)
{
aim += s[i].str;
}
//cout << aim << endl;
int lenth = aim.size();
for (int i = ; i < lenth; i++)
{
if (i == )
{
pre[i] = (aim[i] == 's');
}
else
{
pre[i] = pre[i - ] + (aim[i] == 's');
}
if (aim[i] == 'h')
{
anser += pre[i];
}
}
cout << anser << endl;
return ;
}
E
一般想到的DP思路是DP[i][j]表示前i个用了j块钱能吸引多少鸟 但是这里j太大了(于是题目就提示你C[i]的总值不超过10000)
所以用DP[i][j]表示在前i个吸引了j个鸟最多好剩多少魔力 复杂度:
前面的复杂度是pre[i]较小的时候 后面是pre[i]较大的时候
DP方程:
#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;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
//const int maxn = 3e5 + 10;
const int maxn = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
const int turn2[][] = {{, }, { -, }, {, }, {, -}, {, -}, { -, -}, {, }, { -, }};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll n, W, B, X;
ll c[];
ll pre[];
ll cost[];
ll dp[][];
int main()
{
//freopen("sample.txt", "w", stdout);
// W初始值 B增加的上限 X回复的数目
mem(dp, -);
cin >> n >> W >> B >> X;
dp[][] = W;
for (int i = ; i <= n; i++)
{
cin >> c[i];
pre[i] = pre[i - ] + c[i];
}
for (int i = ; i <= n; i++)
{
cin >> cost[i];
}
for (ll i = ; i <= n; i++)
{
for (ll j = ; j <= pre[i]; j++)
{
for (ll k = ; k <= min(j, c[i]); k++)
{
if (dp[i - ][j - k] == - || 1LL * cost[i]*k > dp[i - ][j - k])
{
continue;
}
dp[i][j] = max(dp[i][j], dp[i - ][j - k] - 1LL * k * cost[i]);
}
if (dp[i][j] != -)
{
dp[i][j] = min(dp[i][j] + X, W + 1LL * j * B);
}
}
}
for (int i = pre[n]; i >= ; i--)
{
if (dp[n][i] >= )
{
cout << i << endl;
return ;
}
}
return ;
}
F
先N*lnN处理出每个数的被除数个数 然后做前缀和 如果K>dp[N] 就无解
所以猜测 当dp[i]>=k时即有解 当数据大于10时 dp数列每两个之间相差不会超过各自之下的i/2+1~i的质数个数(为什么选i/2~i的质数是因为其sum[i]只为1 方便调整)
所以当N>10的时候找到最小的满足dp[i]>=k的i 然后用删去其i/2+1~i之间的质数来调整 dp[i]-k
后来知道 当N<=10的时候 也满足>10的解法 一个数N的被整除数在N^(1/3)数量级 质数则在 N/LogN数量级
#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 turn[][] = {{, }, { -, }, {, }, {, -}};
//priority_queue<int, vector<int>, less<int>> que;
//next_permutation
ll Mod = ;
vector<int> prime;
int n;
ll k;
int anserten;
ll sum[];
ll dp[];
int visit[];
void dfs(int pos, int x)
{
if (pos == n + )
{
if (anserten == )
{
int now = ;
for (int i = ; i <= ; i++)
{
if (x & ( << i))
{
for (int j = i * ; j <= ; j += i)
{
if (x & ( << j))
{
//cout << i << " " << j << endl;
now++;
}
}
}
}
if (now == k)
{
anserten = x;
}
}
return ;
}
dfs(pos + , x + ( << pos));
dfs(pos + , x);
}
priority_queue<pair<int, int> > que;
int main()
{
cin >> n >> k;
for (int i = ; i <= n; i++)
{
for (int j = i * ; j <= n; j += i)
{
sum[j]++;
}
}
for (int i = ; i <= n; i++)
{
dp[i] = dp[i - ] + sum[i];
}
if (k > dp[n])
{
cout << "No" << endl;
return ;
}
else
{
cout << "Yes" << endl;
if (n <= )
{
dfs(, );
int anser = ;
for (int i = ; i <= ; i++)
{
if (anserten & ( << i))
{
anser++;
}
}
cout << anser << endl;
for (int i = ; i <= ; i++)
{
if (anserten & ( << i))
{
cout << i << " ";
}
}
cout << endl;
}
else
{
dp[n + ] = INT_MAX;
int aim;
for (aim = ; aim <= n; aim++)
{
if (dp[aim] >= k && dp[aim - ] < k)
{
break;
}
}
int anser = aim;
for (int i = aim; i >= ; i--)
{
visit[i] = ;
}
for (int i = aim; i >= aim / + ; i--)
{
que.push({sum[i], i});
}
int cha = dp[aim] - k;
pair<int, int> cnt;
while (cha > )
{
cnt = que.top();
que.pop();
if (cnt.first <= cha)
{
cha -= cnt.first;
visit[cnt.second] = ;
anser--;
}
}
cout << anser << endl;
for (int i = ; i <= aim; i++)
if (visit[i])
{
cout << i << " ";
}
cout << endl;
}
}
return ;
}
Codeforces 922 思维贪心 变种背包DP 质因数质数结论的更多相关文章
- Codeforces 922 E Birds (背包dp)被define坑了的一题
网页链接:点击打开链接 Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp ne ...
- Codeforces 730J:Bottles(背包dp)
http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...
- codeforces 148E Aragorn's Story 背包DP
Aragorn's Story Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...
- Buy Low Sell High CodeForces - 867E (思维,贪心)
大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...
- Codeforces 1093C (思维+贪心)
题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...
- 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/ ...
- 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论
一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...
- 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp
题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...
- E. The Contest ( 简单DP || 思维 + 贪心)
传送门 题意: 有 n 个数 (1 ~ n) 分给了三个人 a, b, c: 其中 a 有 k1 个, b 有 k2 个, c 有 k3 个. 现在问最少需要多少操作,使得 a 中所有数 是 1 ~ ...
随机推荐
- Linux 下ThinkPHP项目出现_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Admin/0dfec61edd66f450033aa87c28a760f4.php
在Linux中部署了ThinkPHP项目,访问时却出现了_STORAGE_WRITE_ERROR_:./Application/Runtime/Cache/Admin/0dfec61edd66f450 ...
- 学习Linux的基础网站
http://c.biancheng.net/view/726.html
- 【linux】杀掉进程命令
1.找到对应的进程 通过端口查找 lsof -i:端口号 netstat -tunlp | grep 端口 lsof -i:9500 netstat -tunlp | grep 9500 2. ...
- c++实验7 二叉树
二叉树数据结构表示及基本操作算法实现 1.所加载的库函数或常量定义及类的定义: #include<stdlib.h> #include<stdio.h> #include&qu ...
- ASP.NET对路径"C:/......."的访问被拒绝 解决方法小结 [转载]
问题: 异常详细信息: System.UnauthorizedAccessException: 对路径“C:/Supermarket/output.pdf”的访问被拒绝. 解决方法: 一.在IIS中的 ...
- centos7配置NTP时间服务器
Network Time Protocol--NTP时间服务器,用来同步网络中各个计算机时间的协议. 通常将一台服务器配置为时间服务器,然后集群内其他服务器都来同步这台服务器的时间. 目的:集群内所有 ...
- Neither abstinence nor excess ever renders man happy
inch.n. 英寸 courageous.adj.勇敢的 porcelain.n.瓷器 adj.脆的 inventor. n. 发明者 trivial.adj. 不重要的 grove.n.小树林,果 ...
- pkg-config too old的解决方法
linux下安装一些库时,会提示pkg-config too old,可以尝试下面的命令 apt-get install pkg-config
- 【Linux开发】将cmd中命令输出保存为TXT文本文件
将cmd中命令输出保存为TXT文本文件 在网上看到一篇名为:"[转载]如何将cmd中命令输出保存为TXT文本文件" 例如:将Ping命令的加长包输出到D盘的ping.txt文本文件 ...
- Java中类和接口
很形象的接口的使用——针对初学者 里氏代换原则是什么?听起来很高深,不过我们也不是什么学院派,就不讲大道理了,直接拿个例子来说一下. 我们拿人和程序员举个例子.人是一个大类,程序员是继承自人的子类.看 ...