HDU - 6023 //编译错误不算罚时
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int vis[100];
int num[20];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int id,h1,f1;
char str[5];
int cnt=0,sum=0;
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
for(int i=0;i<m;i++)
{
scanf("%d",&id);
scanf("%d",&h1);
getchar();
scanf("%d",&f1);
scanf("%s",str);
id=id-1000;
if(vis[id]==1)continue;
if(strcmp(str,"AC")==0)
{
vis[id]=1;
sum+=h1*60+f1+num[id];
}
else
num[id]+=20;
}
for(int i=0;i<=n;i++)
if(vis[i]==1)
cnt++;
cout<<cnt<<" "<<sum<<endl;
}
return 0;
}
The total cost consists of two parts. Building a candy shop at classroom ii would have some cost cici. For every classroom PP without any candy shop, then the distance between PP and the rightmost classroom with a candy shop on PP's left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.
Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.
In each test case, the first line contains an integer n(1≤n≤3000)n(1≤n≤3000), denoting the number of the classrooms.
In the following nn lines, each line contains two integers xi,ci(−109≤xi,ci≤109)xi,ci(−109≤xi,ci≤109), denoting the coordinate of the ii-th classroom and the cost of building a candy shop in it.
There are no two classrooms having same coordinate.OutputFor each test case, print a single line containing an integer, denoting the minimal cost.Sample Input
3 1 2 2 3 3 4 4 1 7 3 1 5 10 6 1Sample Output
5 11
题意是,花费的代价是:要是在这个位置上建立糖果屋就花菲建糖果屋的代价,要不就在这个位置上建糖果屋花费的代价就是,这个屋子的坐标减去离他最近的建糖果屋的坐标,还有最左面的屋子一定要建立糖果屋的。
不是贪心的题:
举个反例:
当输入 5
1 2
2 2
3 2
4 3
5 4
的时候,在2这个做标处,按照贪心处理的话不会在这个坐标建糖果屋,花费是: 2+1+2+3+4, 而选择建的话是 2+2+1+2+3+4,所以贪心的思想是错误的。
#include<iostream>
#include<algorithm>
#include<cstring>
#include <cstdio>
//#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
//#define IO ios::sync_with_stdio(false);
using namespace std;
LL dp[3005][2];
struct node
{
LL id;
LL cost;
}s[3005];
bool cmp(node a,node b)
{
return a.id<b.id;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)//不加EOF TLE!
{
for(int i=0;i<n;++i)
{
//cin>>s[i].id>>s[i].cost;
scanf("%lld%lld",&s[i].id,&s[i].cost);
}
sort(s,s+n,cmp);
memset(dp,INF,sizeof(dp));
dp[0][1]=s[0].cost;
for(int i=1;i<n;++i)
{
dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].cost;
LL ans=0;
for(int j=i-1;j>=0;--j)
{
ans+=(s[j+1].id-s[j].id)*(i-j);
dp[i][0]=min(dp[j][1]+ans,dp[i][0]);
}
}
printf("%lld\n",min(dp[n-1][1],dp[n-1][0]));
// cout<<min(dp[n-1][0],dp[n-1][1])<<endl;
}
return 0;
}
#include<iostream>
#include<algorithm>
#include <cstdio>
//#include <bits/stdc++.h>
#include <cstring>
#define INF 0x3f3f3f3f //用1e20就不对
//#define IO ios::sync_with_stdio(false);
using namespace std;
long long dp[3005][2]={0};
long long dis[3005][3005];
struct node
{
int id;
int cost;
bool operator <(const node &b)const
{
return id<b.id;
}
}s[3005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i)
{
scanf("%d%d",&s[i].id,&s[i].cost);
}
sort(s+1,s+n+1);
memset(dis,0,sizeof(dis));
memset(dp,INF,sizeof(dp));
for(int i=1;i<=n;++i)
{
for(int j=1;j<i;++j)
if(i!=j)
dis[j][i]=dis[j][i-1]+s[i].id-s[j].id;//(1+i-j)*(i-j)*0.5;
}
// for(int i=1;i<=n;++i)
// {
// for(int j=i+1;j<=n;++j)
// dis[i][j]=dis[i][j-1]+s[j].id-s[i].id;//(1+i-j)*(i-j)*0.5;
// }
dp[1][1]=s[1].cost;
for(int i=2;i<=n;++i)
{
dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].cost;
for(int j=i-1;j;--j)
{
dp[i][0]=min(dp[i][0],dp[j][1]+dis[j][i]);
}
}
long long MIN=min(dp[n][0],dp[n][1]);
printf("%I64d\n",MIN);
}
return 0;
}
``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.
In each test case, there is an integer n(3≤n≤100000)n(3≤n≤100000) in the first line, denoting the number of integers in the sequence.
Then the following line consists of nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor each test case, print a single line containing a single integer, denoting the maximum GCD.Sample Input
3 3 1 1 1 5 2 2 2 3 2 4 1 2 4 8Sample Output
1 2 2
用公约数的前缀和和后缀方法:
总结:求一串数字的最大公约数和数字的顺序无关,
//#include<iostream>
//#include<algorithm>
//#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
//long long dp[3005][3005]={0};
int num[100005];
int pre[100005];
int nre[100005];
int _gcd(int a,int b)
{
return b?_gcd(b,a%b):a;
}
int main()
{
int Case,n;
cin>>Case;
for(int t=0;t<Case;++t){
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%d",&num[i]);
pre[0]=num[0];
nre[n-1]=num[n-1];
for(int i=1;i<n;++i)
{
pre[i]=_gcd(pre[i-1],num[i]);
}
for(int i=n-2;i>=0;--i)
{
nre[i]=_gcd(nre[i+1],num[i]);
}
int MAX=max(pre[n-2],nre[1]);
for(int i=1;i<n-1;++i)
{
MAX=max(MAX,_gcd(pre[i-1],nre[i+1]));
}
printf("%d\n",MAX);
}
return 0;
}
用的一种找规律的方法,这道题就让去掉一个数字然后求他们的最大公约数,那么你不管怎么去(偏大的还是偏小的)那个数字,剩下的数字的最大公约数肯定是小于这个数字串的最小的那个数字,可以从数字串a的a1位置开始暴力,(要是去掉2个数字,可以从a2位置的数字开始枚举,不过去除的数字太多的话,应该会TLE,所以不如上面的求前缀后缀的方法好)
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
int n;
int a[100005];
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int h=a[1];
int flag=0;
for(int j=h;j>=1;j--)
{
int sum=0;
for(int i=0;i<n;i++)
{
if(a[i]%j==0)
sum++;
if(sum>=n-1)
{
flag=1;
cout<<j<<endl;
break;
}
}
if(flag==1)
break;
}
}
return 0;
}
(队友提到一道题,给你n个数字求他们的最大公因子是多少:从他们的最小的那个数字n开始找到1要是中间的有个数字%==0这个数字串的次数为数字串的个数,就是这个串的最大公约数)
Given two integers nn and kk. Let f(i)=ikf(i)=ik, please evaluate the sum f(1)+f(2)+...+f(n)f(1)+f(2)+...+f(n). The problem is simple as it looks, apart from the value of nnin this question is quite large.
Can you figure the answer out? Since the answer may be too large, please output the answer modulo 109+7109+7.
Each of the following TT lines contains two integers n(1≤n≤10000)n(1≤n≤10000) and k(0≤k≤5)k(0≤k≤5).
OutputFor each test case, print a single line containing an integer modulo 109+7109+7.Sample Input
3 2 5 4 2 4 1Sample Output
33 30 10
大数,
import java.util.*;
import java.math.*;
public class Main{
public static void main(String args[]) {
Scanner cin=new Scanner (System.in);
int n;
n=cin.nextInt();
//BigInteger a;
int a;
int k;
BigInteger MOD=new BigInteger("1000000007");
for(int i=0;i<n;++i)
{
BigInteger sum=BigInteger.ZERO;
a=cin.nextInt();
k=cin.nextInt();
BigInteger j=BigInteger.ONE;//=new BigInteger("1");
for(int m=0;m<a;m++)
{
// System.out.println();
sum=sum.add(j.pow(k));
sum=sum.mod(MOD);
j=j.add(BigInteger.ONE);
}
System.out.println(sum);
}
}
}
用快速幂:
#include<iostream>
#include<algorithm>
#include <cstdio>
using namespace std;
const int MOD = 7+1e9;
//#define MOD 1e9+7 越界了
long long n,k;
long long POW(long long p,long long x)
{
long long ans=1;
while(x)
{
if(x&1)
ans=(ans*p)%MOD;
p=(p*p)%MOD;
x>>=1;
}
return ans;
}
int main()
{
long long t;
cin>>t;
while(t--)
{
long long sum=0;
cin>>n>>k;
for(long long p=1;p<=n;++p)
{
sum=(sum+POW(p,k))%MOD;
}
//printf("%lld\n",sum);
cout<<sum<<endl;
}
return 0;
}
Graph Theory
HDU - 6029Let the set of vertices be {1, 2, 3, ..., nn}. You have to consider every vertice from left to right (i.e. from vertice 2 to nn). At vertice ii, you must make one of the following two decisions:
(1) Add edges between this vertex and all the previous vertices (i.e. from vertex 1 to i−1i−1).
(2) Not add any edge between this vertex and any of the previous vertices.
In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.
Now Little Q is interested in checking whether a ''Cool Graph'' has perfect matching. Please write a program to help him.
In each test case, there is an integer n(2≤n≤100000)n(2≤n≤100000) in the first line, denoting the number of vertices of the graph.
The following line contains n−1n−1 integers a2,a3,...,an(1≤ai≤2)a2,a3,...,an(1≤ai≤2), denoting the decision on each vertice.OutputFor each test case, output a string in the first line. If the graph has perfect matching, output ''Yes'', otherwise output ''No''.
Sample Input
3 2 1 2 2 4 1 1 2Sample Output
Yes No No
意思是给你一个数n代表有n个顶点(1,2,3.....n),然后再给你n-1个数字代表对顶点2,3,4,,,,n所做的操作,操作有两种形式,1和2
1代表把当前顶点(ai)及其之前的顶点都加上一条边,可以放到一个集合里面(边往集合哩放的条件是,这个边的两个顶点在这个集合里面都没有出现过)2这个操作代表这个点是个孤立的顶点不给他加边,然后问到最后那个集合里面是不是包含这n个所有顶点。mmp
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,op;
cin>>n;
// int edge[100005]={0};
int mgj=0;//统计没加进去的点的个数
for(int i=2;i<=n;++i)
{
scanf("%d",&op);//cin>>op;
if(op==1)
{
if(!mgj)
mgj=0;
else
mgj--;
}
else
mgj++;
}
if(n&1)//奇数构成的边肯定要剩下一个顶点孤立
printf("No\n");
else if(!mgj)
printf("Yes\n");// cout<<"Yes"<<endl;
else
printf("No\n");// cout<<"NO"<<endl;
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,order;
cin>>n;
int flag=-1,sum=0;//sum统计加进去的个数
for(int i=2;i<=n;i++)
{
cin>>order;
if(order==1)
{
if(sum<i-1)
sum+=2;
}
}
if(n%2==1)printf("No\n");
else
{
if(sum==n) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}