Codeforces Round #326 (div2)
2015-10-16 22:45
225 查看
1001.
题解:
Idea is a simple greedy, buy needed meat for i - th day when it's cheapest among days
1, 2, ..., n.
So, the pseudo code below will work:
Time complexity:
分析:感觉别人好有想法,贪心遍历一遍就有答案了,看到自己的代码怎一个蠢字了得!!
我的:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
struct p
{
int a,p,pos;
bool vis=false;
}b[100003];
bool cmp(p a,p b)
{
if(a.p==b.p)return a.pos<b.pos;
return a.p<b.p;
}
int main()
{
int n;
LL ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&b[i].a,&b[i].p);
b[i].pos=i;
}
sort(b,b+n,cmp);
for(int i=0;i<n;i++){
if(!b[i].vis){
ans+=b[i].a*b[i].p;
for(int j=i+1;j<n;j++){
if(!b[j].vis&&b[j].pos>b[i].pos){
b[j].vis=true;
ans+=b[j].a*b[i].p;
}
}
}
}
cout<<ans<<endl;
return 0;
}
1002.
题解:
Find all prime divisors of n. Assume they are
p1, p2, ..., pk (in
). If answer is
a, then we know that for each
1 ≤ i ≤ k, obviously a is not divisible by
pi2 (and all greater powers of
pi). So
a ≤ p1 × p2 × ... × pk. And we know that
p1 × p2 × ... × pk is itself lovely. So,answer is
p1 × p2 × ... × pk
Time complexity:
分析:跟我想的一样,求所有质因数相乘就是答案。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
LL n;
cin>>n;
LL ans=1;
for(LL i=2;i*i<=n;i++){
if(n%i==0){
ans*=i;
while(n%i==0)
n/=i;
}
}
if(n>1)ans*=n;
cout<<ans<<endl;
return 0;
}
1003.
题解:不知道题解在说什么鬼,这题只需要计算相同的数字个数,如果能凑够2个x,就相当于一个x+1,按照这一想法敲就好,
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=10000006;
int a[maxn];
int main()
{
// ios::sync_with_stdio(false);
int n,x;
cin>>n;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
scanf("%d",&x);
a[x]++;
}
int ans=0;
for(int i=0;i<maxn;i++){
if(a[i]){
a[i+1]+=a[i]/2;
a[i]%=2;
if(a[i])
ans++;
}
}
cout<<ans<<endl;
return 0;
}
1004.
分析:dp,不会
题解:
Idea is a simple greedy, buy needed meat for i - th day when it's cheapest among days
1, 2, ..., n.
So, the pseudo code below will work:
ans = 0 price = infinity for i = 1 to n price = min(price, p[i]) ans += price * a[i]
Time complexity:
分析:感觉别人好有想法,贪心遍历一遍就有答案了,看到自己的代码怎一个蠢字了得!!
我的:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
struct p
{
int a,p,pos;
bool vis=false;
}b[100003];
bool cmp(p a,p b)
{
if(a.p==b.p)return a.pos<b.pos;
return a.p<b.p;
}
int main()
{
int n;
LL ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&b[i].a,&b[i].p);
b[i].pos=i;
}
sort(b,b+n,cmp);
for(int i=0;i<n;i++){
if(!b[i].vis){
ans+=b[i].a*b[i].p;
for(int j=i+1;j<n;j++){
if(!b[j].vis&&b[j].pos>b[i].pos){
b[j].vis=true;
ans+=b[j].a*b[i].p;
}
}
}
}
cout<<ans<<endl;
return 0;
}
1002.
题解:
Find all prime divisors of n. Assume they are
p1, p2, ..., pk (in
). If answer is
a, then we know that for each
1 ≤ i ≤ k, obviously a is not divisible by
pi2 (and all greater powers of
pi). So
a ≤ p1 × p2 × ... × pk. And we know that
p1 × p2 × ... × pk is itself lovely. So,answer is
p1 × p2 × ... × pk
Time complexity:
分析:跟我想的一样,求所有质因数相乘就是答案。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
LL n;
cin>>n;
LL ans=1;
for(LL i=2;i*i<=n;i++){
if(n%i==0){
ans*=i;
while(n%i==0)
n/=i;
}
}
if(n>1)ans*=n;
cout<<ans<<endl;
return 0;
}
1003.
题解:不知道题解在说什么鬼,这题只需要计算相同的数字个数,如果能凑够2个x,就相当于一个x+1,按照这一想法敲就好,
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn=10000006;
int a[maxn];
int main()
{
// ios::sync_with_stdio(false);
int n,x;
cin>>n;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
scanf("%d",&x);
a[x]++;
}
int ans=0;
for(int i=0;i<maxn;i++){
if(a[i]){
a[i+1]+=a[i]/2;
a[i]%=2;
if(a[i])
ans++;
}
}
cout<<ans<<endl;
return 0;
}
1004.
分析:dp,不会
相关文章推荐
- Error:JDK1.8 Cannot change version of project facet Dynamic Web Module to 3.1.
- swift课程 01 swift 基础
- POJ - 1195 Mobile phones(二维树状数组)
- 可以吧
- 菜鸟之伤--学习总结
- 华为机试
- jquery.validate.unobtrusive
- 与TableView插入、删除、移动、多选,刷新控件
- 14.7-3
- HttpClient的超时用法小记
- HelloWorld项目的目录结构
- 快速发展的Swift是否将淘汰Objective-C?
- 笑笑2222
- Quarts 官方简单示例1
- ASP.NET mvc异常处理的方法
- 笑笑
- OpenCV之光流法跟踪运动目标
- 关于在linux下用c连接数据库的一个小试验
- webView修改UA
- POJ - 2352 Stars(树状数组求解逆序对)