Problem A URAL 2062 Ambitious Experiment
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1001000;
int n;
int a[maxn];
LL C[maxn];
void add(int x, int v)
{
while(x <= n)
{
C[x] += v;
x += x & (-x);
}
}
LL sum(int x)
{
LL ret = 0;
while(x)
{
ret += C[x];
x -= x & (-x);
}
return ret;
}
void solve()
{
int q; scanf("%d", &q);
while(q --)
{
int op, x, l, r;
scanf("%d", &op);
if(op == 1)
{
scanf("%d", &x);
LL ans = a[x];
for(int j = 1; j * j <= x; j ++)
{
if(x % j) continue;
ans += sum(j);
if(j != x / j)
ans += sum(x / j);
}
printf("%I64d\n", ans);
}
else
{
scanf("%d%d%d", &l, &r, &x);
add(l, x);
add(r + 1, -x);
}
}
}
int main()
{
while(scanf("%d", &n) != EOF)
{
CLR(C, 0);
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
solve();
}
return 0;
}
Problem B URAL 2063 Black and White
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
void solve(int x){
for(int i = 1; i <= x; i ++) {
for(int j = i + 1; j <= x; j ++) {
printf("? %d %d\n", i, j);
}
}
int t = rand() % (x - 1) + 1;
printf("! %d %d\n", t, t + 1);
}
int main() {
int m;
scanf("%d", &m);
for(int i = 2; i <= 1 + m; i ++) {
solve(i);
}
return 0;
}
Problem C URAL 2064 Caterpillars
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
#define _max(a, b) a > b ? a : b
using namespace std;
const int _maxn = 1001000;
vector<int> vec[_maxn];
int n;
int a[_maxn];
inline int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
void solve()
{
sort(a, a + n);
n = unique(a, a + n) - a;
int q = Scan();
while(q --)
{
int x = Scan();
int ans = 0;
int b = lower_bound(a, a + n, x) - a;
if(b != n)
{
printf("%d\n", x);
continue;
}
for(int i = 1; i * i <= x; i ++)
{
int b = lower_bound(a, a + n, i) - a;
if(b != n)
{
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
}
if(b >= 1)
{
b --;
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
b ++;
}
if(b < n - 1)
{
b ++;
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
b --;
}
if(i == x / i) continue;
int j = (x + i - 1) / i;
b = lower_bound(a, a + n, j) - a;
if(b != n)
{
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
}
if(b >= 1)
{
b --;
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
b ++;
}
if(b < n - 1)
{
b ++;
int c = x % (a[b] * 2);
if(c <= a[b]) ans = _max(ans, c);
else ans = _max(ans, a[b] * 2 - c);
b --;
}
}
printf("%d\n", ans);
}
return ;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = 1; i <= n; i ++)
{
a[i - 1] = Scan();
}
solve();
}
return 0;
}
Problem D URAL 2065 Different Sums
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
m --;
if(m % 2 == 0) {
int mxx = m / 2;
for(int i = 1; i <= mxx; i ++) {
printf("%d %d ", i, -i);
}
for(int i = mxx * 2 + 1; i <= n; i ++) {
printf("%d ", 0);
}
puts("");
}else{
int mxx = m / 2;
for(int i = 1; i <= mxx; i ++) {
printf("%d %d ", i, -i);
}
printf("%d ", mxx + 1);
for(int i = mxx * 2 + 2; i <= n; i ++) {
printf("%d ", 0);
}
puts("");
}
}
return 0;
}
Problem E URAL 2066 Simple Expression
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
int a, b, c;
int main()
{
while(scanf("%d%d%d", &a, &b, &c) != EOF)
{
if(c + b > c * b)
printf("%d\n", a - b - c);
else printf("%d\n", a - b * c);
}
return 0;
}
Problem F URAL 2067 Friends and Berries
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn=200020;
struct Point{
LL x,y;
int id;
Point(){}
Point(LL x,LL y):x(x),y(y){}
Point operator-(const Point &a){
return Point(x-a.x,y-a.y);
}
LL operator*(Point a){
return x*a.y-y*a.x;
}
void input(int idx){
scanf("%I64d%I64d",&x,&y);
id=idx;
}
bool friend operator<(const Point &a,const Point &b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
}p[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++) p[i].input(i);
sort(p,p+n);
bool ok=1;
for(int i=1;i+1<n&&ok;i++){
if((p[i]-p[0])*(p[i]-p[n-1])!=0) ok=0;
}
if(!ok) puts("0");
else{
puts("1");
printf("%d %d\n",p[0].id+1,p[n-1].id+1);
}
}
}
Problem G URAL 2068 Game of Nuts
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int sum=0;
for(int i=0;i<n;i++){
int tmp;scanf("%d",&tmp);
sum+=(tmp-1)/2;
}
if(sum%2) puts("Daenerys");
else puts("Stannis");
}
}
Problem H URAL 2069 Hard Rock
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 100100;
int n, m;
int a[maxn], b[maxn];
void solve()
{
int ans = min(a[1], b[m]);
ans = max(ans, min(a[n], b[1]));
int mx = a[1];
for(int i = 2; i <= n; i ++)
mx = max(mx, a[i]);
ans = max(ans, min(mx, min(b[1], b[m])));
mx = b[1];
for(int i = 2; i <= m; i ++)
mx = max(mx, b[i]);
ans = max(ans, min(mx, min(a[1], a[n])));
printf("%d\n", ans);
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
for(int i = 1; i <= m; i ++)
scanf("%d", &b[i]);
solve();
}
return 0;
}
Problem I URAL 2070 Interesting Numbers
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn=1000100;
bool isp[maxn];
int pri[maxn/10],l;
void init(){
l=0;
memset(isp,0,sizeof(isp));
for(int i=2;i*i<maxn;i++) if(!isp[i]){
for(int j=i*i;j<maxn;j+=i) isp[j]=1;
}
for(int i=2;i<maxn;i++) if(!isp[i]) pri[l++]=i;
}
LL gao(LL n){
LL ret=n;
for(int i=0;i<l&&1ll*pri[i]*pri[i]<=n;i++){
LL now=1ll*pri[i]*pri[i];
int cnt=2;ret--;
while(now*pri[i]<=n){
now*=pri[i];
cnt++;
if(!isp[cnt+1]) ret--;
}
}
return ret;
}
int main(){
init();
LL L,R;
while(cin>>L>>R){
// printf("%lld %lld\n",gao(L-1),gao(R));
printf("%I64d\n",gao(R)-gao(L-1));
}
}
Problem J URAL 2071 Juice Cocktails
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
string s[101000];
int id[101000];
int out[101000];
// A0,B1,P2;
int cnt[100];
int top[100];
int ans[100];
int main() {
int n;
while(cin >> n) {
memset(cnt, 0, sizeof cnt);
memset(id, 0, sizeof id);
for(int i = 0; i < n; i ++) {
string x;
cin >> s[i];
x = s[i];
if(x.find('A') != -1) id[i] |= 1;
if(x.find('B') != -1) id[i] |= 2;
if(x.find('P') != -1) id[i] |= 4;
// printf("id = %d\n", id[i]);
cnt[id[i]] ++;
}
vector<int> t;
for(int i = 0; i < 8; i ++) {
if(cnt[i]) t.push_back(i);
}
// printf("sz = %d\n", t.size());
int ned = INF;
do{
int tmp = 0;
if(t[0] & 1) tmp ++;
if(t[0] & 2) tmp ++;
if(t[0] & 4) tmp ++;
for(int i = 1; i < t.size(); i ++) {
if((t[i - 1] & 1) == 0 && (t[i] & 1)) tmp ++;
if((t[i - 1] & 2) == 0 && (t[i] & 2)) tmp ++;
if((t[i - 1] & 4) == 0 && (t[i] & 4)) tmp ++;
}
if(tmp < ned) {
ned = tmp;
for(int i = 0; i < t.size(); i ++) {
ans[i] = t[i];
}
}
}while(next_permutation(t.begin(), t.end()));
printf("%d\n", ned);
int hd = 1;
for(int i = 0; i < t.size(); i ++) {
// printf("%d ->\n", ans[i]);
int nowId = ans[i];
top[nowId] = hd;
hd += cnt[nowId];
}
for(int i = 0; i < n; i ++) {
int who = id[i];
out[top[who] ++] = i + 1;
}
for(int i = 1; i <= n; i ++) {
printf("%d%c", out[i], i == n ? '\n' : ' ');
}
}
return 0;
}
/**
7
A
AB
ABP
AP
B
BP
P
*/
Problem K URAL 2072 Kirill the Gardener 3
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1100000;
map<int, int> mp;
vector<int> v[maxn];
struct node{
int id, x;
bool operator < (const node& a) const {
return x < a.x;
}
}s[maxn];
int top;
LL dp[2];
int pos[2];
int main() {
int n;
while(scanf("%d", &n) != EOF) {
top = 0;
mp.clear();
for(int i = 0; i < n; i ++) {
v[i].clear();
}
for(int i = 0; i < n; i ++) {
scanf("%d", &s[i].x);
s[i].id = i + 1;
}
sort(s, s + n);
for(int i = 0; i < n; i ++) {
if(mp.count(s[i].x) == 0) {
mp[s[i].x] = top ++;
}
v[mp[s[i].x]].push_back(s[i].id);
}
for(int i = 0; i < top; i ++) {
sort(v[i].begin(), v[i].end());
}
dp[0] = dp[1] = 0;
pos[0] = pos[1] = 1;
for(int i = 0; i < top; i ++) {
int sz = v[i].size();
LL len = v[i][sz - 1] - v[i][0];
LL dp0 = dp[0], dp1 = dp[1];
dp[0] = min(dp0 + abs(v[i][sz - 1] - pos[0]) + len, dp1 + abs(v[i][sz - 1] - pos[1]) + len) + sz;
dp[1] = min(dp0 + abs(v[i][0] - pos[0]) + len, dp1 + abs(v[i][0] - pos[1]) + len) + sz;
pos[0] = v[i][0];
pos[1] = v[i][sz - 1];
// printf("%lld <-> %lld %d %d\n", dp[0], dp[1], pos[0], pos[1]);
}
LL ans = min(dp[0], dp[1]);
printf("%I64d\n", ans);
}
return 0;
}
Problem L URAL 2073 Log Files
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define xx first
#define yy second
#define LL long long
#define MP make_pair
#define INF 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
char pl[200] = "+------------------------------+--------+-------------+";
char sp[5][200] =
{
"+------------------------------+--------+-------------+",
"|Contest name |Date |ABCDEFGHIJKLM|",
"+------------------------------+--------+-------------+"
};
/**
+------------------------------+--------+-------------+
|Contest name |Date |ABCDEFGHIJKLM|
+------------------------------+--------+-------------+
|Codeforces Gamma Round 512 |29.02.16|ooo.o |
+------------------------------+--------+-------------+
|URKOP |17.10.15|oo..xo.o.o.. |
+------------------------------+--------+-------------+
*/
int n;
char ch[200];
int main()
{
// freopen("L.txt", "r", stdin);
while(scanf("%d", &n) != EOF)
{
getchar();
puts(sp[0]);
puts(sp[1]);
while(n --)
{
puts(pl);
putchar('|');
gets(ch); int len = strlen(ch);
for(int i = 0; i < 30; i ++)
{
if(i < len) putchar(ch[i]);
else putchar(' ');
}
putchar('|');
gets(ch);
for(int i = 0; i < 8; i ++)
putchar(ch[i]);
putchar('|');
int a, b; scanf("%d%d", &a, &b);
int vis[20] = {0};
getchar();
for(int i = 0; i < b; i ++)
{
gets(ch); int p = ch[0] - 'A';
if(ch[2] == 'A')
{
vis[p] = 1;
}
else if(vis[p] == 0)
vis[p] = 2;
}
for(int i = 0; i < 13; i ++)
{
if(i < a)
{
if(vis[i] == 0) putchar('.');
else if(vis[i] == 1) putchar('o');
else putchar('x');
}
else putchar(' ');
}
putchar('|');
puts("");
}
puts(pl);
}
return 0;
}