URAL 2035

输入x,y,c,  找到任意一对a,b 使得a+b==c&& 0<=a<=x && 0<=b<=y

注意后两个条件,顺序搞错wa几次

 #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int x,y,c;
while(~scanf("%d%d%d",&x,&y,&c)){
if(x+y<c){
puts("Impossible");
continue;
}
int a,b;
bool s=false;
if(x>y){
s=true;
swap(x,y);
}
if(x>=c){
a=c;
b=;
}
else{
a=x;
b=c-x;
}
if(s) swap(a,b);
printf("%d %d\n",a,b);
}
return ;
}

URAL 2034

无向图,边权都是1,人从s到f,走最短路,最短路可能有多条。强盗在r,强盗要抢劫的距离是从r到人走的路径的最近距离。因为可能有多条最短路,所以要求强盗到达每条最短路到最近距离,输出其中最大的。

做法 预处理出s到每个点到距离,r到每个点到距离。然后从s开始bfs,step记录的是步数,val记录的是这条路径上距离r的最近距离。

 #include<cstdio>
#include<cstring>
#include<queue>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
struct G {
struct E {
int v,next;
} e[M<<];
int le,head[M];
void init() {
le=;
mt(head,-);
}
void add(int u,int v) {
e[le].v=v;
e[le].next=head[u];
head[u]=le++;
}
} g;
bool vis[M];
void bfs(int s,int d[]) {
mt(vis,);
vis[s]=true;
queue<int> q;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=g.head[u]; ~i; i=g.e[i].next) {
int v=g.e[i].v;
if(!vis[v]) {
vis[v]=true;
d[v]=d[u]+;
q.push(v);
}
}
}
}
int sd[M],rd[M],d[M],ans;
struct Q {
int step,val,id;
} now,pre;
queue<Q> q;
void solve(int s,int f) {
ans=;
mt(d,);
now.step=;
now.id=s;
now.val=rd[s];
while(!q.empty()) q.pop();
q.push(now);
while(!q.empty()) {
pre=q.front();
q.pop();
if(pre.id==f) {
ans=max(ans,pre.val);
continue;
}
int u=pre.id;
for(int i=g.head[u]; ~i; i=g.e[i].next) {
int v=g.e[i].v;
now.val=min(pre.val,rd[v]);
now.step=pre.step+;
if(now.step==sd[v]&&d[v]<now.val) {
d[v]=now.val;
now.id=v;
q.push(now);
}
}
}
}
int main() {
int n,m,u,v,s,f,r;
while(~scanf("%d%d",&n,&m)) {
g.init();
while(m--) {
scanf("%d%d",&u,&v);
g.add(u,v);
g.add(v,u);
}
scanf("%d%d%d",&s,&f,&r);
bfs(s,sd);
bfs(r,rd);
solve(s,f);
printf("%d\n",ans);
}
return ;
}

vector存图会慢一些

 #include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
vector<int> g[M];
bool vis[M];
void bfs(int s,int d[]) {
mt(vis,);
vis[s]=true;
queue<int> q;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
int len=g[u].size();
for(int i=; i<len; i++) {
int v=g[u][i];
if(!vis[v]) {
vis[v]=true;
d[v]=d[u]+;
q.push(v);
}
}
}
}
int sd[M],rd[M],d[M],ans;
struct Q {
int step,val,id;
} now,pre;
queue<Q> q;
void solve(int s,int f) {
ans=;
mt(d,);
now.step=;
now.id=s;
now.val=rd[s];
while(!q.empty()) q.pop();
q.push(now);
while(!q.empty()) {
pre=q.front();
q.pop();
if(pre.id==f) {
ans=max(ans,pre.val);
continue;
}
int u=pre.id;
int len=g[u].size();
for(int i=; i<len; i++) {
int v=g[u][i];
now.val=min(pre.val,rd[v]);
now.step=pre.step+;
if(now.step==sd[v]&&d[v]<now.val) {
d[v]=now.val;
now.id=v;
q.push(now);
}
}
}
}
int main() {
int n,m,u,v,s,f,r;
while(~scanf("%d%d",&n,&m)) {
for(int i=;i<=n;i++) g[i].clear();
while(m--) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
scanf("%d%d%d",&s,&f,&r);
bfs(s,sd);
bfs(r,rd);
solve(s,f);
printf("%d\n",ans);
}
return ;
}

URAL 2033

手机排名按照出现次数,次数相同按照最低价格

 #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
string a,b;
map<string,int> num,val;
map<string,int>::iterator it;
struct G{
string a;
int num,val;
friend bool operator <(const G &a,const G &b){
return a.num>b.num||(a.num==b.num&&a.val<b.val);
}
}now;
vector<G> g;
int cost;
int main(){
num.clear();
val.clear();
for(int i=;i<;i++){
cin>>a>>b>>cost;
num[b]++;
if(val[b]){
val[b]=min(val[b],cost);
}
else{
val[b]=cost;
}
}
g.clear();
for(it=num.begin();it!=num.end();it++){
now.a=it->first;
now.num=it->second;
now.val=val[now.a];
g.push_back(now);
}
sort(g.begin(),g.end());
cout<<g[].a<<endl;
return ;
}

URAL 2031

最多4个连续的能满足的 88 89 90 91

 #include<cstdio>
int main(){
int n,ans[]={,,,};
while(~scanf("%d",&n)){
if(n<){
for(int i=;i<n;i++){
printf("%02d ",ans[i]);
}
}
else{
printf("Glupenky Pierre");
}
puts("");
}
return ;
}

URAL 2029

汗诺塔,一开始都在A ,要移动成输入的状态,需要几步,每次都是考虑最下面的一个,然后相当于把n-1个移开,然后把第n个移动到目标处,移动n个需要2^n-1,把最后一个放到目标需要1

 #include<cstdio>
typedef long long LL;
const int M=;
char a[M];
LL p[M];
int main(){
p[]=;
for(int i=;i<M;i++){
p[i]=p[i-]*;
}
int n;
while(~scanf("%d%s",&n,a)){
LL ans=;
int id=;
for(int i=n;i>=;i--){
int his=a[i-]-'A';
if(his==id) continue;
ans+=p[i-];
id=^^^id^his;
}
printf("%lld\n",ans);
}
return ;
}

URAL 2025

n人分k队,不同队之间的人要打一场比赛,问最多能打几场,把人平均分k份打得最多

 #include<cstdio>
int main(){
int t,n,k,ans;
while(~scanf("%d",&t)){
while(t--){
scanf("%d%d",&n,&k);
int x=n/k;
int y=n%k;
ans=y*(x+)*(n-x-)+(k-y)*x*(n-x);
printf("%d\n",ans/);
}
}
return ;
}

URAL 2024

最多存在k种不同字母,问最多几个,有几种方法达到最多

最多几个就是把前k种加起来

方法数就是有几种个数和第k种一样,那么从中选几种

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=1e5+;
char a[M];
int num[];
int C[][];
int main(){
for(int i=;i<;i++){
C[i][i]=;
C[i][]=;
}
for(int i=;i<;i++){
for(int j=;j<i;j++){
C[i][j]=C[i-][j]+C[i-][j-];
}
}
int k;
while(~scanf("%s%d",a,&k)){
mt(num,);
for(int i=;a[i];i++){
num[a[i]-'a']++;
}
sort(num,num+);
int sum=;
int kk=;
for(int i=;i>=;i--){
sum+=num[i];
kk++;
if(kk==k) break;
}
printf("%d ",sum);
if(k==){
puts("");
continue;
}
if(num[-k]==){
puts("");
continue;
}
if(num[-k-]!=num[-k]){
puts("");
continue;
}
int cn=,cm=;
for(int i=;i<;i++){
if(num[i]==num[-k]){
cn++;
}
}
for(int i=-k;i<;i++){
if(num[i]==num[-k]){
cm++;
}
}
printf("%d\n",C[cn][cm]);
}
return ;
}

end

URAL的更多相关文章

  1. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  2. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  3. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  6. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

  10. ural 2064. Caterpillars

    2064. Caterpillars Time limit: 3.0 secondMemory limit: 64 MB Young gardener didn’t visit his garden ...

随机推荐

  1. js中forEach无法跳出循环?

    1. forEach() forEach() 方法从头至尾遍历数组,为每个元素调用指定的函数.如上所述,传递的函数作为forEach()的第一个参数.然后forEach()使用三个参数调用该 函数:数 ...

  2. Windows7下Microsoft Office Excel 不能访问文件解决方案

    1).开始--〉运行--〉cmd 2)命令提示符下面,输入mmc -32,打开32的控制台 3).文件菜单中,添加删除管理单元--〉组件服务 4).在"DCOM配置"中找到&quo ...

  3. [INS-41112] Specified network interface doesnt maintain connectivi

    OS: Oracle Linux Server release 6.3 DB: Oracle 11.2.0.3 安装11.2.0.3.0的RAC,在安装GRID时报错: [INS-41112] Spe ...

  4. HTML5的placeholder属性如何实现换行

    在HTML5中,placeholder是一个非常有用的属性,当控件中无内容时可以代替UI控件的提示功能,而不需要写额外的代码.但如果有一个textarea控件,我们需要多行的文本提示信息时,使用”\n ...

  5. Unity3d Shortcuts

    参考:http://www.ceeger.com/Manual/ 场景视图导航  Click-drag to drag the camera around. 点击拖拽平移场景视图 Hold Alt a ...

  6. ubuntu 修改ssh远程主机名称,mac开机运行命令,静默方式启动virtual box虚拟机,静默执行run脚本

    一.修改主机名 ssh登陆 vi /etc/hostname vi /etc/hosts hostname ulocal (执行这个命令,无须重启服务器) 保证127.0.0.1 的hostname与 ...

  7. System.IO之内存映射文件共享内存

    内存映射文件是利用虚拟内存把文件映射到进程的地址空间中去,在此之后进程操作文件,就 像操作进程空间里的地址一样了,比如使用c语言的memcpy等内存操作的函数.这种方法能够很好的应用在需要频繁处理一个 ...

  8. supplicant

    概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...

  9. iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化

    /** 1:Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录 2:tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除 3 ...

  10. 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据

    在请求方法的顶部添加        [ValidateInput(false)]就OK了 从客户端(Content=" sdfdddd ...")中检测到有潜在危险的 Reques ...